.Net String.Format() to Add Commas in Thousands Place For a Number

.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

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

This is a culture thing. In your locale, space is the thousands specifier, apparently. I see commas. To see a specific locale's output, specify that explicitly. A very common option is "invariant":

var total = string.Format(CultureInfo.InvariantCulture, "{0:n0}", 12345);

How do I format a number with commas?

Try N0 for no decimal part:

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

add commas using String.Format for number and

You can do this, which I find a bit cleaner to read the intent of:

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

Example:

string.Format("{0:#,###0}", 123456789); // 123,456,789
string.Format("{0:#,###0}", 0); // 0

String.Format decimal with both thousand separators and forced decimal places

In a custom numeric format string a period (.) is used for a "localized decimal separator". Ie. even if your current locale uses a comma as a decimal separator you should use a period in a format string. Similarly comma (,) is used for the localised thousands separator.

As your format puts the comma after the period things are going to get confused (thousands separators don't apply after the decimal point).

So try:

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

(Using # for digits to only include if input is large enough.)

Separate number with comma for thousands asp.net

IF you are looking for Currency use

DataFormatString="{0:c}"

otherwise use

DataFormatString="{0:N2}"

Try

    Text='<%# Eval("Applied_Amount_Varience_Sum","{0:c}") %>'

or

    Text='<%# Eval("Applied_Amount_Varience_Sum","{0:N2}") %>'

Add comma thousand separator to decimal (.net)

You should use a custom formatting like #,##0.00

string s = string.Format("{0:#,##0.00}", xx);

Will produce 1,234.50 when xx = 1234.5M

forget about converting to double, that won't really help.

Format a number to display a comma when larger than a thousand

Take a look at The Numeric ("N") Format Specifier

General use:

Dim dblValue As Double = -12445.6789
Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture))
' Displays -12,445.68

If you are only using integers then the following:

Dim numberString As String = 1234.ToString("N0")

Will show numberString = "1,234" as the "N0" format will not add any figures after a decimal point.

String.Format - integer, thousands separator, no decimal

N0 is the format you are looking for.
N will format it with commas, and the 0 is to tell it to not use decimals:

// will format as 33,540
string.Format("{0:N0}", 33540.54M)

// example using an integer: 1,200
string.Format("{0:N0}", 1200);

From MSDN:

Result: Integral and decimal digits, group separators, and a decimal
separator with optional negative sign. Supported by: All numeric
types. Precision specifier: Desired number of decimal places. Default
precision specifier: Defined byNumberFormatInfo.NumberDecimalDigits.
More information: The Numeric ("N") Format Specifier.



Related Topics



Leave a reply



Submit