1

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.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • 4
    Displaying trailing zeroes is a format operation. Trailing zeroes aren't significant in numbers. 120d and 120.000d are identical. The first example requires rounding, eg `Math.Round(d,3,MidPointRounding.ToZero)` – Panagiotis Kanavos Apr 01 '22 at 07:09
  • I do understand this, but isn't there any way in which I can achieve this? its a clients requirement. @pan – Syed Raza Abbas SRA Apr 01 '22 at 07:12
  • Achieve what? What are the *actual* requirements? The question is self-contradictory. In math, trailing decimal zeros aren't significant. They aren't stored anywhere. There's no difference at all between 120 and 120.00. You *have* to use `Format` if you want to display a fixed number of decimals – Panagiotis Kanavos Apr 01 '22 at 07:14
  • There was a Question Asked almost similar... [Formatting a float to 2 decimal places](https://stackoverflow.com/questions/6356351/formatting-a-float-to-2-decimal-places) – Sana T. Apr 01 '22 at 07:15
  • Is the *actual* requirement to display real numbers without rounding? – Panagiotis Kanavos Apr 01 '22 at 07:21
  • The ACTUAL requirement is to display the double values with 3 decimal places regardless of them having 3 decimals. They want 120 as 120.000 and 12.3 as 12.300 and so on. These values needs to be displayed on DevExpress Reports. The issue with string formatting is that functions like Sumrunning() are applied on these values over reports which cannot work on string fields. So if I formatted 120 to 120.000 then further calculations couldn't be performed on reports. @PanagiotisKanavos – Syed Raza Abbas SRA Apr 01 '22 at 07:25
  • 1
    So this *is* a formatting requirement. In all report engines you can specify cell formatting at the field and even cell level – Panagiotis Kanavos Apr 01 '22 at 07:26
  • I didn't know about cell formatting. Let me try that and reply you again. Thanks for the help brother. – Syed Raza Abbas SRA Apr 01 '22 at 07:28

2 Answers2

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

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
0

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
Nick Vu
  • 14,512
  • 4
  • 21
  • 31