Possible Duplicate:
Round a double to 2 significant figures after decimal point
I need at most N decimals, no more, but I don't want trailing zeroes. For example, if N = 2 then
15.352
15.355
15.3
15
should become (respectively)
15.35
15.36
15.3
15
Try Math.Round(value, 2).ToString()
Math.Round(15.352, 2).ToString(); //15.35
Math.Round(15.355, 2).ToString(); //15.36
Math.Round(15.3, 2).ToString(); //15.3
Math.Round(15.0, 2).ToString(); //15
The second paramater for round is for you to specify how many decimal places to round to. It will round up by default.
This can be done by using a custom format string, such as "0.##", which displays a maximum two decimal places.
String.Format("{0:0.##}", 123.4567); // "123.46"
Reference: http://www.csharp-examples.net/string-format-double/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With