HasanG
HasanG

Reputation: 13161

Separate long numbers by 3 digits

Is there a easy way to transform 1000000 in 1.000.000? A regex or string format in asp.net, c#

Upvotes: 10

Views: 8020

Answers (5)

Fredrik Mörk
Fredrik Mörk

Reputation: 158319

You can use ToString together with a formatting string and a format provider that uses '.' as a group separator and defines that the number should be grouped in 3-digit groups (which is not the case for all cultures):

int number = 1000000;
Console.WriteLine(number.ToString("N0", new NumberFormatInfo()
                                            {
                                                NumberGroupSizes = new[] { 3 },
                                                NumberGroupSeparator = "."
                                            }));

Upvotes: 13

egrunin
egrunin

Reputation: 25063

I think you're asking about culture-specific formatting. This is the Spanish way, for example:

1000000.ToString("N", CultureInfo.CreateSpecificCulture("es-ES"));

Upvotes: 7

RichK
RichK

Reputation: 11879

Using ToString("N") after will convert 1000000 to 1,000,000. Not sure about . though

Upvotes: 4

Thomas Levesque
Thomas Levesque

Reputation: 292465

1000000.ToString("N0")

Upvotes: 8

Aryabhatta
Aryabhatta

Reputation:

Use ToString with numeric format string after reading into an integer. I believe the one you are looking for is "N" and its relatives.

MSDN page about numeric format strings: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

Upvotes: 2

Related Questions