本文介绍了将数字转换为特定文化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像202667.4
这样的数字.我想根据文化将其转换为数字 .
I have a number like 202667.4
. I want to convert this to number based on culture.
例如:
任何帮助将不胜感激.
谢谢.
推荐答案
如果要以区域性特定的格式表示现有数字(例如,double
),请尝试格式化:
If you want to represent existing number (say, double
) in culture specific format, try formatting:
https://docs .microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings
double source = 202667.4;
// "n" - ... group separators, and a decimal separator with optional negative sign
// "de" - German culture
string result = source.ToString("n", CultureInfo.GetCultureInfo("de"));
Console.WriteLine(result);
结果
202.667,40
如果给您一个string
并且想要一个数字,请输入Parse
(TryParse
):
If you are given a string
and you want a number, put Parse
(TryParse
):
string data = "202.667,40";
double result = double.Parse(data, CultureInfo.GetCultureInfo("de"));
Console.WriteLine(data.ToString(CultureInfo.InvariantCulture));
如果不想每次使用格式设置时都指定区域性,则可以将区域性设置为当前之一:
If you don't want to specify the culture each time you work with formatting, you can set the culture as a current one:
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("de");
...
double source = 202667.4;
Console.WriteLine($"{source:n}");
这篇关于将数字转换为特定文化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!