How to Format a Number in C# With Commas and Decimals

Format a number with commas and decimals in C# (asp.net MVC3)

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

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

How do I format a number in C# with commas and decimals?

Not sure (and unable to test right now) but would something like this work?

"#,##0.################"

.NET String.Format() to add commas in thousands place for a number

String.Format("{0:n}", 1234);  // Output: 1,234.00
String.Format("{0:n0}", 9876); // No digits after the decimal point. Output: 9,876

Formatting Numbers as Strings with Commas in place of Decimals

I think:

string.Format(System.Globalization.CultureInfo.GetCultureInfo("de-DE"), "{0:0.0}", 4.3); 

should do what you want.

Format a number with commas, but keep decimals

This appears to do exactly what you want:

public void Code(params string[] args)
{
Print(1000);
Print(100000);
Print(123.456);
Print(100000.21 );
Print(100200.123456);
}

void Print(double n)
{
Console.WriteLine("{0:###,###.#######}", n);
}

1,000
100,000
123.456
100,000.21
100,200.123456

How do I format a number with commas?

Try N0 for no decimal part:

string formatted = a.ToString("N0"); // 10,000,000

String Format for up to 2 decimal places or simple integer and with thousand separator - C#

String.Format("{0:#,##0.##}", 123.0);


Related Topics



Leave a reply



Submit