views:

96

answers:

5

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

+2  A: 

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

Moron
+2  A: 
1000000.ToString("N0")
Thomas Levesque
value.ToString("N0").Replace(',','.'); does the trick. Also comma or dot comes from system separator setting. For any case I'm replacing comma with dot. Thank you.
HasanGursoy
Won't work in India though (they don't group in 3-digit groups)
Fredrik Mörk
@Fredrik, it will work according to regional settings, whatever they are
Thomas Levesque
@Thomas: exactly, and the title of the question is *"Separate long numbers by 3 digits"*. If your code runs in the 'hi-IN' culture the output will be `10,00,000`.
Fredrik Mörk
+3  A: 

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

RichK
That will probably depend on culture settings. In Turkey it should default to dots rather than commas, so you should be ok.
Daniel Dyson
+4  A: 

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

1000000.ToString("N", CultureInfo.CreateSpecificCulture("es-ES"));
egrunin
+5  A: 

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 = "."
                                            }));
Fredrik Mörk
That is so cool!
RichK
Cool does not always imply correct. egrunin's solution is far better as that is the proper "global" way! Of course, we don't really know what the OP wanted :)
Moron