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
2010-05-17 10:04:24
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
2010-05-17 10:10:36
Won't work in India though (they don't group in 3-digit groups)
Fredrik Mörk
2010-05-17 10:17:04
@Fredrik, it will work according to regional settings, whatever they are
Thomas Levesque
2010-05-17 10:28:54
@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
2010-05-17 10:56:21
+3
A:
Using ToString("N")
after will convert 1000000 to 1,000,000. Not sure about . though
RichK
2010-05-17 10:05:45
That will probably depend on culture settings. In Turkey it should default to dots rather than commas, so you should be ok.
Daniel Dyson
2010-05-17 10:09:06
+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
2010-05-17 10:12:39
+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
2010-05-17 10:14:17