'Prevent double rounding in C# (and Blazor Server)?

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;
        }
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source