views:

70

answers:

2

In India and other Asian countries money is formatted as following: The first three digits grouped in three then all other digits are grouped in pair of two. eg : 2,54,255.12 5,22,54,255.12 etc string money = String.Format("{0:#,##0.00}", 254255.12);

gives the output 254,255.12

but the output required is 2,54,255.12

+12  A: 

Use an appropriate CultureInfo and the "c" format specifier:

CultureInfo hindi = CultureInfo.CreateSpecificCulture("hi-IN");
string text = string.Format(hindi, "{0:c}", 254255.12);

Note that you should really use decimal rather than double for currency values, to avoid binary floating point issues.

Jon Skeet
+1 never use floating point numbers for currency calculations.
slebetman
You mean don't use binary floating point... `decimal` is still a floating point type.
Jon Skeet
Is *binary* the right term here? Aren't `decimal` values represented in memory in binary? I believe `decimal` is represented as a 16-byte (binary) integer and associated (binary) scaling factor. It's the lack of separate mantissa and exponent that reduces rounding errors etc.
MarkJ
@MarkJ: It's whether the floating point is a binary point or a decimal point which is important. `decimal` and `double` both have separate mantissa and exponent values. Where do you see the difference being? (Btw, `decimal` actually has a 5 bit exponent, a 96-bit mantissa and a sign bit.)
Jon Skeet
See http://pobox.com/~skeet/csharp/floatingpoint.html and http://pobox.com/~skeet/csharp/decimal.html for more information about the differences.
Jon Skeet
Arghh! Binary point vs. decimal point, of course! Sorry, I completely misread that. I think it's time to go home and lie down in a dark room now.
MarkJ
+1  A: 

This is a straightforward method:

System.Globalization.CultureInfo ci = 
   System.Globalization.CultureInfo.GetCultureInfo("hi-IN");
Console.WriteLine((123456789.87).ToString("N", ci));

Notice that this is accomplished with a correctly configured NumberFormatInfo structure in the format provider / culture object. You can create your own culture objects too, if need be:

foreach (int gs in ci.NumberFormat.CurrencyGroupSizes)
{
   Console.WriteLine(gs);
}

Also note that if the system is configured so that hi-IN is the native culture on the machine, numbers will be formatted that way by default without having to explicitly retrieve the culture and pass it to the format provider argument.

BlueMonkMN