我有一个使用不同语言的网站。在模型中,我具有以下成员声明:
[Required(ErrorMessageResourceType = typeof(ViewRes.GlobalResource), ErrorMessageResourceName = "awr1")]
[Range(typeof(decimal), "0.00100001", "10000", ErrorMessageResourceType = typeof(ViewRes.GlobalResource), ErrorMessageResourceName = "TotalMoneyMinMaxValidation")]
public decimal TotalMoney { get; set; }
将UI文化更改为
ru-RU
时,出现以下错误:0.00100001不是十进制的有效值。
如何将我的区域性信息和UI保留为
ru-RU
,但所有小数点都用点号(。)代替逗号(,)?这是我尝试过的:
CultureInfo ci = new CultureInfo("ru-RU");
ci.NumberFormat.NumberDecimalSeparator = ".";
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
不起作用...
最佳答案
由于RangeAttribute
是使用运行时区域性转换的,因此没有精确的解决方法。您需要使用双精度来接受精度损失:
[Range(0.01, 10000)]
或完全放弃使用
RangeAttribute
。请参见this link。
关于c# - 区域性更改后如何在小数点ToString处保留“。”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19935686/