Search code examples
c#decimal

Using Decimal.ToString() without rounding up the number


I have following decimal number

string number = "60.9";
Response.Write(Convert.ToDecimal(number).ToString("#,0")); // prints 61

I want to format the value like 99,999,999... but if the value has decimal point, it is getting rounded up

Please let me know how can I print 60.9, instead of 61 with same number formatting


Solution

  • You can use N format. This will show the number with group separator of your locale (, for US locale for example, although you can optionally influence that), and leave decimal part as it is:

    Convert.ToDecimal(number).ToString("N")
    

    Update. After some discussion in comments the approach that meets all requirements appeared to be

    Convert.ToDecimal(number).ToString("#,#.##########", CultureInfo.InvariantCulture)
    

    Kudos to Jon.