防止 C#(和 Blazor 服务器)中的双重舍入?
这是我的第一个问题。
我有一个数据类型为 double 的数字值,它在设置器中设置之前对十进制数进行四舍五入。在数字输入字段中,仅允许 20 个字符(不包括逗号)。这在函数“CheckInput”中进行检查并返回布尔值。但是,由于数字在小数点后四舍五入,因此我无法正确检查字符数。有谁知道如何防止双精度自动舍入?
这是我的双重属性:
[Required]
[StringLength(20, ErrorMessage = "Es sind max. 20 Zeichen erlaubt (ohne Komma).")]
private double _xValue;
public double XValue
{
get => _xValue;
set
{
var oldValue = value;
_xValue= value;
if (!CheckInput(InputField.XValue))
{
_xValue= oldValue;
}
}
}
这是检查我输入的数字的函数:
public bool CheckInput(InputField inputField)
{
if (inputField == InputField.XValue || inputField == InputField.YValue)
{
var xWertDecimal = Convert.ToDecimal(XValue);
var yWertDecimal = Convert.ToDecimal(YWert);
var valueString = String.Empty;
if (inputField == InputField.XValue) valueString = xWertDecimal.ToString();
else if (inputField == InputField.YValue) valueString = yWertDecimal.ToString();
var splittedString = valueString.Split(',');
if (splittedString.Length == 2)
{
if (splittedString[0].Length + splittedString[1].Length > 20)
{
Snackbar.Add("Max. 20 Zeichen erlaubt!");
return false;
}
}
else if (splittedString.Length == 1)
{
if (splittedString[0].Length > 20)
{
Snackbar.Add("Max. 20 Zeichen erlaubt!");
return false;
}
}
return true;
}
else if (inputField == InputField.Haeufigkeit)
{
if (Haeufigkeit <= 0)
{
Snackbar.Add("Häufigkeit muss größer als 0 sein!");
return false;
}
return true;
}
else
{
return true;
}
}
This is my first question here.
I have a number value with the data type double, which rounds the decimal number before it is set in the setter. In the input field of the number only 20 characters are allowed (without comma). This is checked in the function "CheckInput" and the boolean is returned. However, since the number is rounded after the decimal point, I am unable to check for the number of characters correctly. Does anyone have any idea how to prevent automatic rounding on the double?
This is my double Property:
[Required]
[StringLength(20, ErrorMessage = "Es sind max. 20 Zeichen erlaubt (ohne Komma).")]
private double _xValue;
public double XValue
{
get => _xValue;
set
{
var oldValue = value;
_xValue= value;
if (!CheckInput(InputField.XValue))
{
_xValue= oldValue;
}
}
}
This is the function to check my input number:
public bool CheckInput(InputField inputField)
{
if (inputField == InputField.XValue || inputField == InputField.YValue)
{
var xWertDecimal = Convert.ToDecimal(XValue);
var yWertDecimal = Convert.ToDecimal(YWert);
var valueString = String.Empty;
if (inputField == InputField.XValue) valueString = xWertDecimal.ToString();
else if (inputField == InputField.YValue) valueString = yWertDecimal.ToString();
var splittedString = valueString.Split(',');
if (splittedString.Length == 2)
{
if (splittedString[0].Length + splittedString[1].Length > 20)
{
Snackbar.Add("Max. 20 Zeichen erlaubt!");
return false;
}
}
else if (splittedString.Length == 1)
{
if (splittedString[0].Length > 20)
{
Snackbar.Add("Max. 20 Zeichen erlaubt!");
return false;
}
}
return true;
}
else if (inputField == InputField.Haeufigkeit)
{
if (Haeufigkeit <= 0)
{
Snackbar.Add("Häufigkeit muss größer als 0 sein!");
return false;
}
return true;
}
else
{
return true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无需将数字转换为十进制即可获取字符串值。
您可以只使用 double 本身的
ToString()
方法,并检查那里的位数。这是一个返回双精度数字的解决方案:
这是您的代码的快速调整版本:
PS 我没有机会测试它,因为它需要您端的其他代码。如果有任何错误,请告诉我。
No need to convert the numbers to Decimal for getting the string value.
You can just use the
ToString()
method of the double itself, and check the amount of digits there.Here is a solution that would return the number of the digits of the double:
And here's the quickly adjusted version of your code:
P.S. I didn't have a chance to test it as it requires the other code that you have on your end. If there's any error, please let me know.