[英]RangeValidator cannot work with decimal value (c#, Enterprise Library)
Hello Stackoverflow fellas! 您好Stackoverflow家伙!
I'm working with Enterprise Library 5.0 and I'm trying to validate if a decimal value is from the specified range, but there's no obvious way to do it 我正在使用Enterprise Library 5.0,正在尝试验证十进制值是否在指定范围内,但是没有明显的方法
eg 例如
[RangeValidator(0, RangeBoundaryType.Exclusive, 1, RangeBoundaryType.Ignore)]
works only for int, double & float. 仅适用于int,double和float。
I'm exactly looking for the equivalent of the Visual Basic's line of code: 我正在寻找与Visual Basic的代码行等效的代码:
<RangeValidator(GetType(Decimal), "0.00", RangeBoundaryType.Inclusive, "0.00", RangeBoundaryType.Ignore, MessageTemplate := "Value must be greater than 0.")>
I don't want to cast, because of the precision. 由于精确度,我不想投射。 Has anyone overcame this?
有人克服了吗?
I'm not sure if this will work but try making your range values decimal: 我不确定这是否行得通,但尝试将范围值设置为十进制:
[RangeValidator(0.0, RangeBoundaryType.Exclusive, 1.0, RangeBoundaryType.Ignore)]
If that doesn't work, try: 如果那不起作用,请尝试:
[RangeValidator(0.0m, RangeBoundaryType.Exclusive, 1.0m, RangeBoundaryType.Ignore)]
The RangeValudatorAttribute
class doesn't have a constructor that supports decimals directly - VB may indirectly cast the RangeValudatorAttribute
类没有直接支持小数的构造函数-VB可能会间接转换
You can use the constructor that takes double
values instead: 您可以改用采用
double
值的构造函数 :
[RangeValidator(0.0, RangeBoundaryType.Exclusive, 1.0, RangeBoundaryType.Ignore)]
Since 0.0
and 1.0
can be represented exactly in binary you shouldn't have a precision issue when comparing to decimal
values. 由于
0.0
和1.0
可以精确地用二进制表示,因此与decimal
值进行比较时不会出现精度问题。
我已经通过以下十进制方式解决了它:
[RangeValidator(typeof(decimal), "0.00", RangeBoundaryType.Inclusive, "1.00", RangeBoundaryType.Ignore)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.