Captain Comic
Captain Comic

Reputation: 16186

Need to convert double or decimal to string

I need to convert double to string with two decimal digits separated with 'dot' My concern is that dot must be always used as separator.

Upvotes: 5

Views: 3116

Answers (2)

Ali Safari
Ali Safari

Reputation: 1775

Perhaps to avoid messing up with CultureInfo settings on clients systems, we better set a concrete way to force the machine to use dot as decimal separator and not thousand separator ==> regardless of culture! So,

NumberFormatInfo fi= new NumberFormatInfo();
fi.NumberDecimalSeparator = ".";
string doubleDotDecimalNr = doubleNr.ToString(fi);

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499770

The simplest way is to specify CultureInfo.InvariantCulture as the culture, e.g.

string text = d.ToString("N2", CultureInfo.InvariantCulture);

Upvotes: 13

Related Questions