skip to Main Content

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


  1. You can use Math.Round to achieve it

    var value = Math.Round(12.878999, 3, MidpointRounding.ToZero);
    

    For the integer type, you can do it this way

    var value = 129 + 0.000m; //extend 3 decimals for an integer number
    
    Login or Signup to reply.
  2. 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 print 120 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

    enter image description here
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search