Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Double to String: Culture specific

Tags:

c#

Let's say my method has a argument of type Double. but sometimes what I pass is an int like 7 and not a double like 7.0 , so if I use ToString method then 7 will be "7" , but I want it to be "7.0", now I also need to consider the culture because some times maybe French is using "," for decimal so it should be "7,0" ... how can I do this conversion to string?

like image 493
Bohn Avatar asked Dec 13 '22 13:12

Bohn


2 Answers

Are you looking for this?

double x = 7.0;

String Display = x.ToString("0.0", CultureInfo.CreateSpecificCulture("de-DE"));
like image 61
Steve Wellens Avatar answered Dec 15 '22 03:12

Steve Wellens


Besides all the correct answers to your question I'd like to hint you into using CultureInfo.InvariantCulture for any scenario where you are about to store data into a file, database etc.

Had I known this earlier, it would have saved me lots of time and pain.

like image 37
yas4891 Avatar answered Dec 15 '22 03:12

yas4891