This is what I want to do:
if a double field has more than 3 decimal places then it should convert to 3 decimal figures and if no decimal places are present then it should convert to 3 decimals
e.g 12.878999 -> 12.878
120 -> 120.000
I cannot use string.Format() as I want the double field to stay double.
2
Answers
You can use
Math.Round
to achieve itFor the integer type, you can do it this way
The first example requires Math.Round, eg
Math.Round(d,3,MidPointRounding.ToZero)
.The second isn’t meaningful. Trailing decimal zeroes aren’t significant. In the real types (float, double) they don’t affect the storage of the number. The call
Math.Round(120d, 3, MidpointRounding.AwayFromZero)
will print120
without a format string.Displaying a double with three trailing zeroes is a formatting operation.
Update
From the comments it appears the actual problem is how to format a report sum in DevExpress Reports. All report engines allow specifying a format for fields and cells.
The Format Data page in the DevExpress Reports docs shows how to modify the
FormatString
property for a specific value