Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display numeric in 3 digit grouping

Tags:

c#

How to display a numeric numbers in 3 digit groupings.

For Ex: 1234 to be 1,234 or 1 234

like image 616
Santhosh Avatar asked Dec 09 '09 07:12

Santhosh


People also ask

How do I change the grouping digits in excel?

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.

What is digit grouping symbol?

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 «'».

What is numeric format example?

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.

Where is number group in Word?

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.


2 Answers

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 
like image 88
Alek Davis Avatar answered Nov 03 '22 00:11

Alek Davis


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, " ");
like image 38
Andrew Hare Avatar answered Nov 02 '22 23:11

Andrew Hare