.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
Any Difference Between "Await Task.Run(); Return;" and "Return Task.Run()"
Observablecollection Not Noticing When Item in It Changes (Even With Inotifypropertychanged)
What Is an "Index Out of Range" Exception, and How to Fix It
Extension Method and Dynamic Object
Generating HTML Email Body in C#
How to Iterate Over a Dictionary
C# Lambda Expressions: Why Should I Use Them
How to Save Application Settings in a Windows Forms Application
Why Covariance and Contravariance Do Not Support Value Type
What Are Your Favorite Extension Methods For C# (Codeplex.Com/Extensionoverflow)
How to Add Extension Methods to an Existing Static Class
Best Way to Keep Track and Iterate Through Tabs and Windows Using Windowhandles Using Selenium
How to Create an Excel (.Xls and .Xlsx) File in C# Without Installing Microsoft Office