57

I need to display a number with commas and a decimal point.

Eg: Case 1 : Decimal number is 432324 (This does not have commas or decimal points).
Need to display it as: 432,324.00.
Not: 432,324

Case 2 : Decimal number is 2222222.22 (This does not have commas).
Need to display it as: 2,222,222.22

I tried ToString("#,##0.##"), but it is not formatting it correctly.

Krishan
  • 2,356
  • 6
  • 36
  • 47
  • 3
    When you say "it is not working", what is the string.Format actually doing? – Tomas McGuinness Apr 16 '13 at 11:09
  • i did not try string.Format method and i thing using tostring formatting is easy – Krishan Apr 16 '13 at 11:15
  • 2
    I really don't understand your examples, what type of formatting is that ? (Or is it just me ) – V4Vendetta Apr 16 '13 at 11:18
  • What @tomasmcguinness is saying, is what result do you get that is "not working"? If you really want to format "123456789" as "34,94,30,94,32,324.00" then you are far from achieving that. – John Willemse Apr 16 '13 at 11:18
  • 3
    LOL so what is the result of your ToString formattings? If you want 123456789 displayed as 34,94,30,94,32,324.00 : Is there some magical logic here that only the wizard of Oz could understand? :D – Paul Zahra Apr 16 '13 at 11:19
  • 2
    For people looking for the case where it ignores the .00 only if it is .00, it is : `{0:#,0.##}` – Worthy7 Oct 04 '16 at 08:41
  • You can use it for 2 decimal points like this. `ToString("#,##0.#0")` https://dotnetfiddle.net/USmzUL – Nahid Dec 09 '21 at 06:20

12 Answers12

114
int number = 1234567890;
number.ToString("#,##0.00");

You will get the result 1,234,567,890.00.

Roys
  • 1,596
  • 2
  • 13
  • 8
  • in model, it's like public decimal Amount{get;set;} , when i use to enter to database value 29.1234 i get result in view 29.12 even i change the database decimal(18,4) which was (18,2) but still i get value like 29.1200 but i want exact value with out any change is it possible – SAR Dec 12 '16 at 11:54
  • what the hell, this answer is accepted?? it gives me 1 234 567 890.00 – Toolkit Aug 26 '17 at 17:48
  • There is no need for `Convert.ToDecimal`, when `number.ToString("#,##0.00")` gives the same result. – Jeppe Stig Nielsen Jul 04 '20 at 17:00
  • 4
    @Toolkit The current culture of the thread determines what separators to use. Your culture uses spaces. You may want to use the overload of `.ToString` that takes an additional argument, namely the format provider. For format provider, you may pass a culture, like `CultureInfo.InvariantCulture` or for example `CultureInfo.GetCultureInfo("en-US")`. Or you may pass an instance of `NumberFormatInfo`. – Jeppe Stig Nielsen Jul 04 '20 at 17:06
  • int number = 1234567890; string formattedNumber = number.ToString("n2", CultureInfo.InvariantCulture); – WiiLF Dec 11 '22 at 03:45
39

Maybe you simply want the standard format string "N", as in

number.ToString("N")

It will use thousand separators, and a fixed number of fractional decimals. The symbol for thousands separators and the symbol for the decimal point depend on the format provider (typically CultureInfo) you use, as does the number of decimals (which will normally by 2, as you require).

If the format provider specifies a different number of decimals, and if you don't want to change the format provider, you can give the number of decimals after the N, as in .ToString("N2").

Edit: The sizes of the groups between the commas are governed by the

CultureInfo.CurrentCulture.NumberFormat.NumberGroupSizes

array, given that you don't specify a special format provider.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
  • This seems like the simplest answer, especially if you want the formatting to be correct regardless of culture settings. – jmatthias Sep 30 '21 at 14:55
12

Try with

ToString("#,##0.00")

From MSDN

*The "0" custom format specifier serves as a zero-placeholder symbol. If the value that is being formatted has a digit in the position where the zero appears in the format string, that digit is copied to the result string; otherwise, a zero appears in the result string. The position of the leftmost zero before the decimal point and the rightmost zero after the decimal point determines the range of digits that are always present in the result string.

The "00" specifier causes the value to be rounded to the nearest digit preceding the decimal, where rounding away from zero is always used. For example, formatting 34.5 with "00" would result in the value 35.*

Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
11

I had the same problem. I wanted to format numbers like the "General" format in spreadsheets, meaning show decimals if they're significant, but chop them off if not. In other words:

1234.56 => 1,234.56

1234 => 1,234

It needs to support a maximum number of places after the decimal, but don't put trailing zeros or dots if not required, and of course, it needs to be culture friendly. I never really figured out a clean way to do it using String.Format alone, but a combination of String.Format and Regex.Replace with some culture help from NumberFormatInfo.CurrentInfo did the job (LinqPad C# Program).

string FormatNumber<T>(T number, int maxDecimals = 4) {
    return Regex.Replace(String.Format("{0:n" + maxDecimals + "}", number),
                         @"[" + System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator + "]?0+$", "");
}   

void Main(){
    foreach (var test in new[] { 123, 1234, 1234.56, 123456.789, 1234.56789123 } )
        Console.WriteLine(test + " = " + FormatNumber(test));
}

Produces:

123 = 123
1234 = 1,234
1234.56 = 1,234.56
123456.789 = 123,456.789
1234.56789123 = 1,234.5679
Community
  • 1
  • 1
Wade Hatler
  • 1,785
  • 20
  • 18
  • 1
    This is the best answer I've personally seen. I'm using a slightly modified version of this in an extension method. Great job! – Tyler Kalosza Oct 08 '19 at 23:13
11

Try with

ToString("#,##0.###")

Produces:

1234.55678 => 1,234.556

1234 => 1,234

Community
  • 1
  • 1
Abolfazl Rastgou
  • 655
  • 10
  • 13
10

For Razor View:

$@string.Format("{0:#,0.00}",item.TotalAmount)
JoshYates1980
  • 3,476
  • 2
  • 36
  • 57
8
CultureInfo us = new CultureInfo("en-US");
TotalAmount.ToString("N", us)
DarkoM
  • 335
  • 4
  • 5
4

Your question is not very clear but this should achieve what you are trying to do:

decimal numericValue = 3494309432324.00m;
string formatted = numericValue.ToString("#,##0.00");

Then formatted will contain: 3,494,309,432,324.00

Belogix
  • 8,129
  • 1
  • 27
  • 32
4

All that is needed is "#,0.00", c# does the rest.

Num.ToString("#,0.00"")

  • The "#,0" formats the thousand separators
  • "0.00" forces two decimal points
Matt Welson
  • 258
  • 2
  • 7
2

If you are using string variables you can format the string directly using a : then specify the format (e.g. N0, P2, etc).

decimal Number = 2000.55512016465m;
$"{Number:N}" #Outputs 2,000.55512016465

You can also specify the number of decimal places to show by adding a number to the end like

$"{Number:N1}" #Outputs 2,000.5
$"{Number:N2}" #Outputs 2,000.55
$"{Number:N3}" #Outputs 2,000.555
$"{Number:N4}" #Outputs 2,000.5551
Marcello B.
  • 4,177
  • 11
  • 45
  • 65
0
string Mynewcurrency = DisplayIndianCurrency("7743450.00");
        private string DisplayIndianCurrency(string EXruppesformate) 
        { 
        string fare = EXruppesformate;
        decimal parsed = decimal.Parse(fare, CultureInfo.InvariantCulture);
        CultureInfo hindi = new CultureInfo("en-IN");
     // string text = string.Format(hindi, "{0:c}", parsed);if you want <b>Rs 77,43,450.00</b>
    string text = string.Format(hindi, "{0:N}", parsed); //if you want <b>77,43,450.00</b>
      return ruppesformate = text;
    }
Sagar Mahajan
  • 191
  • 1
  • 4
0

For anyone looking at this now, and getting the "No overload for method 'ToString' takes 1 argument" when using:

TotalNumber.ToString("N")

My solution has been to use :

TotalNumber.Value.ToString("N")

I often get stuck on this when working directly inside an MVC View, the following wasn't working:

@Model.Sum(x => x.Number).ToString("N")

Whereas this works:

@Model.Sum(x => x.Number).Value.ToString("N")
ouflak
  • 2,458
  • 10
  • 44
  • 49
Nancy
  • 1