开发者

Convert Double to String: Culture specific

开发者 https://www.devze.com 2023-03-02 20:56 出处:网络
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 ToStr开发者_StackOverflowing method then 7 will be \"7\" , but I

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 ToStr开发者_StackOverflowing 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?


Are you looking for this?

double x = 7.0;

String Display = x.ToString("0.0", CultureInfo.CreateSpecificCulture("de-DE"));


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.


It already is culture sensitive, it pays attention to the Thread.CurrentCulture property. That won't be English when a French user is running your program. Don't do anything special unless you know for a fact that the default culture is not correct.


you need to use

string.format

msdn

http://msdn.microsoft.com/en-us/library/system.string.format.aspx

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消