-3

I want to Roun d Up Values Like This...

0.1 will be 0

0.3 will be 0

0.5 will be 1

0.9 will be 1

Is there any way to solve it in c#?

Thanks in Advance...

user2357112
  • 260,549
  • 28
  • 431
  • 505
Nik
  • 25
  • 6
  • 3
    Have you tried anything? [Searching google perhaps](http://stackoverflow.com/questions/8844674/how-to-round-to-the-nearest-whole-number-in-c-sharp)? – Yuval Itzchakov Nov 26 '15 at 08:11
  • textbox? - thats typically a string try `Math.Round(Double.Parse("0.1"))` – Henrik Nov 26 '15 at 08:16
  • @YuvalItzchakov no sir i have not tried anything.. – Nik Nov 26 '15 at 09:05
  • @Henrik ok sir..sir but i'm really new in this so what is the role of Double.parse is? – Nik Nov 26 '15 at 09:09
  • @Nik going from strings to numbers.. string is objects that holds a list of characters, in this case: `"0.1" = '0','.','1','\0'` (4 characters, if old fashioned c-styled character array) or `"0.1" = 3, '0','.','1'` (1 length indicator, three characters if FORTRAN styled array) none of those data are possible to do math on. Double.parse tries to interpret a char array as a number.. it works if character array have a formatting, which corresponds to how you would write the number on screen. it fails if the string is "not a number" – Henrik Nov 26 '15 at 09:15
  • @Henrik Ok Got it..Thank you for this brief explaination about that...Thanks – Nik Nov 26 '15 at 09:18

2 Answers2

1

You are looking for Math.Round you can read more about Round

If the fractional component of number is halfway between two integers, one of which is even and the other odd, the even number is returned. Note that this method returns a Decimal instead of an integral type.

and as suggested by @Micky you should be using MidpointRounding.AwayFromZero like

Math.Round(0.5d, MidpointRounding.AwayFromZero));

to get the correct output

Mohit S
  • 13,723
  • 6
  • 34
  • 69
0

You should try using the Decimal.Round method available in C#. For more info, please look at the following link,

https://msdn.microsoft.com/en-us/library/6be1edhb(v=vs.110).aspx

Thanks.