Globalized custom number formatting - Variable decimal points

荒凉一梦 提交于 2019-12-23 20:53:13

问题


I'm trying to alter the existing number formatting in my company's application to make it more readable for international users. This is a stock trading application, so most stock prices come in with numbers precise to 2 decimal points, like so-> 17.23 We could also get ticks in that have precision out to 4 decimal points, so a penny stock might be 0.0341. The original string format that we were using for stocks was "#,##0.00##" Which would give us the format we wanted (essentially trimming '0's). The problem here is the ',' and '.' are forced onto the user, where in many other countries the thousands separator is '.' and the decimal point is ','. Boss man doesn't want to use "N4" for all numbers, even though this would resolve the globalization issue. Is it possible to have a globalized custom string format?

Other options besides writing some middle man code to internationalize numbers formatted the original way or another string.format method?


回答1:


The , and . in the custom format string already translate into the correct separators for the current culture. You should not need to change your code. See MSDN.




回答2:


Check this out:

float value = 0.0341f;
string output = string.Empty;
CultureInfo brazil = new CultureInfo("pt-BR");
CultureInfo usa = new CultureInfo("en-US");
output = value.ToString("C4", brazil.NumberFormat); //output is R$ 0,0341
output = value.ToString("C4", usa.NumberFormat); //output is $0.0341


来源:https://stackoverflow.com/questions/5902450/globalized-custom-number-formatting-variable-decimal-points

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