简体   繁体   English

RangeValidator无法使用十进制值(C#,企业库)

[英]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.01.0可以精确地用二进制表示,因此与decimal值进行比较时不会出现精度问题。

我已经通过以下十进制方式解决了它:

[RangeValidator(typeof(decimal), "0.00", RangeBoundaryType.Inclusive, "1.00", RangeBoundaryType.Ignore)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 rangevalidator C#如何稍后在代码中使用 - rangevalidator c# how to use later in code 无法将十进制值转换为货币,C#-&gt; SP SQL Server - Cannot convert a decimal value to money, C# -> SP SQL Server 使用Microsoft Enterprise Library检查C#中存储过程的返回值是否为空 - Check if return value from stored procedure is null in C# using Microsoft Enterprise Library 如何从企业库(c#)中的ExecuteNonQuery获取输出参数值? - How to get output parameter value from ExecuteNonQuery in enterprise library (c#)? 如何使用C#在GridView中为RangeValidator动态添加MaximumValue? - How to dynamically add MaximumValue for RangeValidator in GridView using c#? 使用企业库时C#非托管应用程序崩溃 - C# Unmanaged application crash when using Enterprise Library RangeValidator货币值小数后不能超过2位数? - RangeValidator Currency value can't contain more than 2 digits after decimal? c# 无法转换匿名类型十进制? [] 到十进制数组 - c# Cannot convert anonymous type decimal? [] to decimal array C#,运算符 &#39;??&#39; 不能应用于“十进制”和“十进制”类型的操作数 - C#, Operator '??' cannot be applied to operands of type 'decimal' and 'decimal' 在C#控制台项目中使用企业库DAAB - Using Enterprise Library DAAB in C# Console Project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM