框架4.0 Asp.net应用
Framework 4.0 Asp.net application
当我运行code我得到一个错误值999.9999的RangeValidator的MaximumValue属性无法转换为类型货币。
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'.
下面是我的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' />
请解释一下我的币值没有能够小数点后包含超过2位数? 除非我怎样才能解决这个问题呢?
Please explain me is currency value can't contain more than 2 digits after decimal? Unless how can I resolve this issue?
推荐答案
的RangeValidator使用NumberFormatInfo.CurrencyDecimalDigits属性,以确定如果字符串可以转换为货币,否则会抛出你的异常。从 MSDN :
在一个RangeValidator控件的类型属性被设置为 货币,在MinimumValue和MaximumValue属性必须是 如在所描述的设置在格式 NumberFormatInfo.CurrencyDecimalDigits,,否则一个例外是 抛出
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.
默认为大多数文化(包括 InvariantCulture的
)是2(阿拉伯国家有3个,但没有4)。
The default for most cultures (incl. InvariantCulture
) is 2 (arabic countries have 3 but none 4).
那么,你使用的是什么文化?如果存放超过两个小数位的货币是非常重要的,那么你可以在这个页面中使用自定义的的NumberFormatInfo
:
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 NumberFormatInfo
in this page:
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;
}
(注意,您需要添加使用System.Globalization;
在顶部)
相关推荐
最新文章