views:

27

answers:

1

Hello, I want to format a number using ToString(CultureInfo.InvariantCulture) and also to 5 decimal places, which can be done using ToString("N5"). How can I do both together?

Thanks :)

+6  A: 

How about using the overload which takes both a format and a culture:

decimal m = 123.4567890123m;
string x = m.ToString("N5", CultureInfo.InvariantCulture);

(Obviously substitute double for decimal if you're using that; there's an equivalent overload.)

Jon Skeet
Ahm yes, didnt see the wood for the trees :D THANKS
grady
What if it can be 3 digits before the decimal separator? Like 100.123456 or 70.1234? Is there a way to have the correct amount of decimal places according to that without writing custom code? I am allowed to have 11 chars all together including the separator. The number I get is 13 chars.
grady
@grady: It's not clear to me what the problem is. 100.123456 will be formatted to 100.12345. To get 11 characters you'd have to have 5 digits before the decimal separator, so you should be okay up to 99999.99999.
Jon Skeet
@grady: You should update your question so that it mentions the 11 chars limit. But you must have custom code for that feature. In general case the code must choose between Nx and Ex taking into account the sign, digits before decimal point and digits after it.
Dialecticus