The solution to the confusion of floating-point numbers in the German environment

The solution to the confusion of floating-point numbers in the German environment

problem symptoms

The products developed by the company need to be exported to achieve internationalization, but the products sent out cannot display normal Text in Germany. After testing, it is found that the color of the font is set according to the local configuration file, and the local configuration parameters are floating point numbers. , when reading, read 0.234 as 234, resulting in an error in the color value of Text.

Finally, after investigation, I found that in China and the United Kingdom, decimals are represented by decimal points, that is: 0.123,
but in Germany, decimals are represented by commas, that is, 0,123. Therefore, if you set the Windows system language to German, you will find that if it is 0.123, After reading, it will become 0,123. If you use float.Parse("0.123"), then the read will be 123.

Solution

1. Pass in the second parameter of float.Parse
var culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
culture.NumberFormat.NumberDecimalSeparator = ".";
float number = float.Parse("0.54", culture);
Set the placeholder for the decimal point to '.'.

2.
Set the formatting format of numbers, symbols, currency, dates, etc. that affect the current thread to Chinese or English.

System.Globalization.CultureInfo.DefaultThreadCurrentCulture = new System.Globalization.CultureInfo(“en-US”);
System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = new System.Globalization.CultureInfo(“en-US”);

Reference link:
https://www.cnblogs.com/handboy/p/3670370.html
https://stackoverflow.com/questions/27722032/c-sharp-float-parse-string

Guess you like

Origin blog.csdn.net/weixin_43295183/article/details/107367269