ValueValidator RangeValidator cannot have more than two digits after the decimal number?
Framework 4.0 Asp.net Application
When I run the code, I got the error "The value" 999.9999 "of the MaximumValue property" RangeValidator "cannot be converted to type" 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 to me that a currency value cannot have more than two digits after the decimal number? If you can't solve this problem?
source to share
RangeValidator
uses NumberFormatInfo.CurrencyDecimalDigits
to determine if the string can be converted to currency, otherwise it will throw your exception. From MSDN :
when the TypeValidator Control Type property is set to Currency, the MinimumValue and MaximumValue properties must be supplied in a format such as that described in NumberFormatInfo.CurrencyDecimalDigits, or an exception will be thrown .
The default for most cultures (including InvariantCulture
) is 2 (Arab countries have 3, but not 4).
What culture are you using? If it is important to store more decimal places than two in a currency, you can use the custom one NumberFormatInfo
on 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)
source to share