Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
For example, a number like 17.3 is being rounded to 20.0.
OR suppose the number is 11.5 is being also rounded to 20.0
OR suppose the number is 10.3 is being also rounded to 10.0
Posted
Updated 23-Dec-14 18:56pm
v2
Comments
Maciej Los 24-Dec-14 6:58am    
Based on what condition?
Why 10.3 do you want to "round" to 10, but 11.5 to 20?

1 solution

Instead of rounding, think about string formatting of numeric values with desired rounding. This is not exactly the same thing. Pure explicit rounding can be dangerous, because one could, by some reason, try to use rounded value and end up with loss of precision. There are very few cases when such rounding is needed (in random number generators, for example). Normally, you only need rounded value when you show it on scree, or something like that. So, you can just use formatting. For the type System.Double, for example, you will need this: http://msdn.microsoft.com/en-us/library/system.double.tostring%28v=vs.110%29.aspx[^].

For format specifiers, please see:
http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx[^].

If you don't want to learn it all, you can use the formatting utility: https://code.msdn.microsoft.com/NET-Framework-4-Formatting-9c4dae8d[^].

—SA
 
Share this answer
 
Comments
Maciej Los 24-Dec-14 6:56am    
Sergey, note that OP wants to roud 17.3 and 11.5 to 20.0. I don't know any string formatting function which could round 11.5 to 20.0. Did you?
Sergey Alexandrovich Kryukov 24-Dec-14 10:29am    
Thanks to you, just noted. But I don't know the purpose or any reasonable rules for "rounding" this way. This is not "rounding".

OP is advised to give appropriate explanations and realize that some set of examples is not a definition.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900