-1

Good day

How to i format a decimal without rounding it

TotalNumber.ToString("0,0", CultureInfo.InvariantCulture)

or

TotalNumber.ToString("#,#")

These 2 strings are rounding 11822252.79 to 11,822,253

The results must be 11,822,252,79 or 11 822 252 79

Shonie
  • 87
  • 8
  • 2
    refer [https://stackoverflow.com/questions/26857926/how-to-format-a-decimal-value-without-rounding-to-store-into-nullable-decimal-va][1] – Aswin TK Apr 18 '18 at 12:50
  • 2
    Multiply by 100 first? Are you mixing the thousandth separator and the decimal separator? One should be a comma and the other a period. – juharr Apr 18 '18 at 12:51
  • 2
    Possible duplicate of [How to format a decimal value WITHOUT rounding to store into nullable decimal Variable in c#](https://stackoverflow.com/questions/26857926/how-to-format-a-decimal-value-without-rounding-to-store-into-nullable-decimal-va) – Linda Lawton - DaImTo Apr 18 '18 at 12:54
  • 1
    the Culture you use has `,` as thousanths seperator. You are not rounding, your are telling it to display only thousands. Use `"#,###.00"` and Invariant culture to get `11,822,252.79` – Patrick Artner Apr 18 '18 at 13:13
  • 1
    `The results must be 11,822,252,79 or 11 822 252 79` Um, are you deliberately mixing up decimal and thousand separators there? – Nyerguds Apr 18 '18 at 13:18

1 Answers1

2

The Invariant Culture has , as thousanths seperator and . as decimals seperator.

You are not rounding, your format strings tells it to display only thousands, no decimals at all.

Use "#,###.00" and Invariant culture to get 11,822,252.79:

using System;
using System.Globalization;

public class Program
{
    public static void Main()
    {
        var n = 11822252.79m;

        Console.WriteLine( n.ToString("#,###.00", CultureInfo.InvariantCulture));
        Console.ReadLine(); 
    }
}

Output:

11,822,252.79

See:

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69