Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting Numbers as Strings with Commas in place of Decimals

I have the following number: 4.3

I'd like to display this number as 4,3 for some of our European friends.

I was under the impression that the following line would do the trick:

string ret = string.Format("{0:0,0}", 4.3); // returns "04", not "4,3"

Am I using the incorrect string?

like image 814
DaveDev Avatar asked Oct 13 '09 09:10

DaveDev


2 Answers

I think:

string.Format(System.Globalization.CultureInfo.GetCultureInfo("de-DE"), "{0:0.0}", 4.3); 

should do what you want.

like image 109
JDunkerley Avatar answered Oct 20 '22 08:10

JDunkerley


NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = ",";
nfi.NumberGroupSeparator = ".";

double num = 4.3;
string ret = num.ToString(nfi);    // 4,3
like image 44
LukeH Avatar answered Oct 20 '22 06:10

LukeH