Format a number with X decimal places and InvariantCulture?

半腔热情 提交于 2020-12-05 05:07:28

问题


I want to format a number using ToString(CultureInfo.InvariantCulture) and also to 5 decimal places, which can be done using ToString("N5"). How can I do both together?


回答1:


How about using the overload which takes both a format and a culture:

decimal m = 123.4567890123m;
string x = m.ToString("N5", CultureInfo.InvariantCulture);

(Obviously substitute double for decimal if you're using that; there's an equivalent overload.)




回答2:


In case you have Decimal not Double:

string.Format(CultureInfo.InvariantCulture, "{0:f5}", m)

as Decimal.ToString() does not have these overloads



来源:https://stackoverflow.com/questions/3966430/format-a-number-with-x-decimal-places-and-invariantculture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!