How to display a numeric numbers in 3 digit groupings.
For Ex: 1234
to be 1,234
or 1 234
In the Data Type Format dialog box, do one of the following: To add a digit grouping symbol to the number, select the Use a digit grouping symbol check box, under Other options. To remove a digit grouping symbol from the number, clear the Use a digit grouping symbol check box, under Other options.
Digit grouping. For ease of reading, numbers with many digits may be divided into groups using a delimiter, such as comma "," or dot ".", half-space (or thin space) " ", space " ", underbar "_" (as in maritime "21_450") or apostrophe «'».
The numeric ("N") format specifier converts a number to a string of the form "-d,ddd,ddd. ddd…", where "-" indicates a negative number symbol if required, "d" indicates a digit (0-9), "," indicates a group separator, and "." indicates a decimal point symbol.
On the Home tab, in the Paragraph group, click Numbering. Note: To select a different number format, right-click a number in the list, point to Numbering, click Define New Number Format, and then select the options that you want.
From the The Numeric ("N") Format Specifier section in Standard Numeric Format Strings:
double dblValue = -12445.6789;
Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture));
// Displays -12,445.68
Console.WriteLine(dblValue.ToString("N1",
CultureInfo.CreateSpecificCulture("sv-SE")));
// Displays -12 445,7
int intValue = 123456789;
Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture));
// Displays 123,456,789.0
Try this:
String.Format("{0:n0}", yourNumber);
If you want spaces instead of commas try this:
String.Format("{0:n0}", yourNumber).Replace(",", " ");
Edit: Marc makes a good point about being cuturally aware so this would be a better way to replace the group separators:
String.Format("{0:n0}", yourNumber)
.Replace(NumberFormatInfo.CurrentInfo.NumberGroupSeparator, " ");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With