防止 C#(和 Blazor 服务器)中的双重舍入?

发布于 2025-01-09 02:29:12 字数 2050 浏览 1 评论 0原文

这是我的第一个问题。

我有一个数据类型为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天生の放荡 2025-01-16 02:29:12

无需将数字转换为十进制即可获取字符串值。
您可以只使用 double 本身的 ToString() 方法,并检查那里的位数。

这是一个返回双精度数字的解决方案:

public static int GetDigitAmount(double numberToCheck)
{
    // For more info on conversion see: https://learn.microsoft.com/en-us/dotnet/api/system.double.tostring?view=net-6.0
    var strValue = numberToCheck.ToString("G", System.Globalization.CultureInfo.InvariantCulture);
    return strValue.Length - (strValue.Contains(".") ? 1 : 0); 
}

这是您的代码的快速调整版本:

public bool CheckInput(InputField inputField)
{
    if (inputField == InputField.XValue || inputField == InputField.YValue)
    {
        double? valueToCheck = null;
        
        if (inputField == InputField.XValue) valueToCheck = XValue;
        else if (inputField == InputField.YValue) valueToCheck = YWert;
        
        if (valueToCheck != null && GetDigitAmount(valueToCheck.Value) > 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;
    }
}

public static int GetDigitAmount(double numberToCheck)
{
    // For more info on conversion see: https://learn.microsoft.com/en-us/dotnet/api/system.double.tostring?view=net-6.0
    var strValue = numberToCheck.ToString("G", System.Globalization.CultureInfo.InvariantCulture);
    return strValue.Length - (strValue.Contains(".") ? 1 : 0); 
}

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:

public static int GetDigitAmount(double numberToCheck)
{
    // For more info on conversion see: https://learn.microsoft.com/en-us/dotnet/api/system.double.tostring?view=net-6.0
    var strValue = numberToCheck.ToString("G", System.Globalization.CultureInfo.InvariantCulture);
    return strValue.Length - (strValue.Contains(".") ? 1 : 0); 
}

And here's the quickly adjusted version of your code:

public bool CheckInput(InputField inputField)
{
    if (inputField == InputField.XValue || inputField == InputField.YValue)
    {
        double? valueToCheck = null;
        
        if (inputField == InputField.XValue) valueToCheck = XValue;
        else if (inputField == InputField.YValue) valueToCheck = YWert;
        
        if (valueToCheck != null && GetDigitAmount(valueToCheck.Value) > 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;
    }
}

public static int GetDigitAmount(double numberToCheck)
{
    // For more info on conversion see: https://learn.microsoft.com/en-us/dotnet/api/system.double.tostring?view=net-6.0
    var strValue = numberToCheck.ToString("G", System.Globalization.CultureInfo.InvariantCulture);
    return strValue.Length - (strValue.Contains(".") ? 1 : 0); 
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文