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