Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to convert double or decimal to string

Tags:

string

c#

.net

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.

like image 681
Captain Comic Avatar asked Oct 19 '10 17:10

Captain Comic


People also ask

Can you convert a double to a string?

We can use StringBuilder and StringBuffer append function to convert double to string.

How do you convert a decimal to a string?

To convert a Decimal value to its string representation using a specified culture and a specific format string, call the Decimal. ToString(String, IFormatProvider) method.

What is the method in converting double to string?

The toString() method of the class Double To convert Double value to String. Read the required primitive double value in to the Double class reference variable (autoboxing happens). Convert it into a String using the toString() method.


2 Answers

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

string text = d.ToString("N2", CultureInfo.InvariantCulture);
like image 174
Jon Skeet Avatar answered Nov 09 '22 22:11

Jon Skeet


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);
like image 31
Ali Safari Avatar answered Nov 10 '22 00:11

Ali Safari