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

前端 未结 1 1020
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 10:30

Framework 4.0 Asp.net application

When I run the code I got a error \"The value \'999.9999\' of the MaximumValue property of \'RangeValidator\' cannot be c

相关标签:
1条回答
  • 2021-01-14 11:06

    The RangeValidator uses the NumberFormatInfo.CurrencyDecimalDigits property to determine if the string can be converted to a currency, otherwise it will throw your exception. From 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.

    The default for most cultures (incl. InvariantCulture) is 2 (arabic countries have 3 but none 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 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;
    }
    

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

    0 讨论(0)
提交回复
热议问题