vb.net RangeValidator 货币值的小数点后不能超过 2 位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14643347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:08:03  来源:igfitidea点击:

RangeValidator Currency value can't contain more than 2 digits after decimal?

c#asp.net.netvb.net

提问by user1118064

Framework 4.0 Asp.net application

框架 4.0 Asp.net 应用程序

When I run the code I got a error "The value '999.9999' of the MaximumValue property of 'RangeValidator' cannot be converted to type 'Currency'.

当我运行代码时,出现错误“'RangeValidator'的MaximumValue属性的值'999.9999'无法转换为'Currency'类型。

Below is my code:

下面是我的代码:

<asp:RangeValidator Runat='server' ControlToValidate='textEdit' 
    MinimumValue='0.0001'
    MaximumValue='999.9999' Type='Currency' 
    ErrorMessage='Should be between 0.0001 and 999.9999' id="idValidtor" 
 display='None' />

Please explain me is currency value can't contain more than 2 digits after decimal? Unless how can I resolve this issue?

请解释我是货币价值不能包含超过 2 位小数点后?除非我怎样才能解决这个问题?

回答by Tim Schmelter

The RangeValidatoruses the NumberFormatInfo.CurrencyDecimalDigitsproperty to determine if the string can be converted to a currency, otherwise it will throw your exception. From MSDN:

RangeValidator使用NumberFormatInfo.CurrencyDecimalDigits属性来确定该字符串是否可以转换为货币,否则会抛出你的异常。从MSDN

when a RangeValidator control's Type property is set to "Currency", the MinimumValue and MaximumValue properties must be provided in a format such as that described in NumberFormatInfo.CurrencyDecimalDigits, otherwise an exception is thrown.

当 RangeValidator 控件的 Type 属性设置为“Currency”时,必须以 NumberFormatInfo.CurrencyDecimalDigits 中描述的格式提供 MinimumValue 和 MaximumValue 属性,否则将引发异常

The default for most cultures (incl. InvariantCulture) is 2 (arabic countries have 3 but none 4).

大多数文化(包括InvariantCulture)的默认值是 2(阿拉伯国家有 3 但没有 4)。

So what culture are you using? If it is important to store more decimal places than two in a currency, then you could use a custom NumberFormatInfoin this page:

那么你使用的是什么文化?如果在货币中存储多于两位的小数位很重要,那么您可以NumberFormatInfo在此页面中使用自定义:

protected void Page_PreInit(object sender, EventArgs e)
{
    var customCulture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
    var nfi = (NumberFormatInfo)customCulture.NumberFormat.Clone();
    nfi.CurrencyDecimalDigits = 4;
    customCulture.NumberFormat = nfi;
    System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
}

(note that you need to add using System.Globalization;at the top)

(注意需要using System.Globalization;在顶部添加)