Bodega Pangal
Bodega Pangal

Reputation: 141

how to validate that a floating number is not "0"

I have an <Entry> control where the user is allowed to enter decimal numbers, for examenple ...

0,2

0,02

5,405

But I do not want to enter a "0" (as a decimal), as follows

0,0

0,00

00,00

The control used in my Vista is an

MyView.XAML:

 <Entry
          HorizontalOptions="FillAndExpand"    
          Placeholder="Cantidad"
          Keyboard="Numeric"
          MaxLength="5"
          Text="{Binding CantidadEstimado}"></Entry>

To then capture the value with a string type in the following way in my ViewModel

ViewModel.CS:

    string cantidadEstimado;

   public string CantidadEstimado
        {
            get
            {
                return cantidadEstimado;
            }
            set
            {
                if (cantidadEstimado != value)
                {
                    cantidadEstimado = value.setOnlyNumbersDouble();
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CantidadEstimado)));
                }
            }
        }

As seen in my Property Amount Dear I have the call to the method setOnlyNumberDouble (), which gives the user the possibility to enter a single comma (","), I attach the following method ...

 public static string setOnlyNumbersDouble(this string s)
    {
        string sTemp = "";

        foreach (var item in s)
        {
            if (item == ',')
            {
                if (!sTemp.Contains(","))
                {
                    sTemp += item;
                }
            }
            else
            {
                sTemp += item; 
            }
        }
        return Regex.Replace(sTemp, @"[^0-9,]+", "");
    }

How can I validate that the user does not enter a "0" as a decimal? Can I reuse my setOnlyNumberDouble () method? any help for me?

Upvotes: 2

Views: 743

Answers (2)

0xAA55
0xAA55

Reputation: 409

To determine if a floating point number value is exactly equals to zero or not, just use the '==' comparator to zero, and make sure the type of the comparand must be exactly the same. like below:

double some_double = 0.0;
if (some_double == 0.0) do_something;

But the code above only run 'do_something' when 'some_double' is directly assigned to an exact number zero, is not what you need.

If 'some_double' were assigned by the result of some arguments (for example, converting a string to a floating point number value) which literally should be zero, the code above won't work as we thought since the value of 'some_double' couldn't be exactly zero due to precise loss of a floating point number.

At this point, we shouldn't just compare a floating value to zero if the floating value was assigned by some computing expressions. Another way to determine if it "is" zero is to compare the absolute value of the floating point number to a very small value, which can be considered to zero.

There are machine epsilons for this purpose, the value of float/double/long double is below:

 FLT_EPSILON = 1.192093e-07
 DBL_EPSILON = 2.220446e-16
LDBL_EPSILON = 1.084202e-19

But these values were too small which is the exact smallest value of each floating point number type. The values were not suitable for the usage of comparing a number which is small enough to be considered as zero since the precise loss of the floating point numbers is more significant than these machine epsilon values, see the link below:

Floating point arithmetic and machine epsilon

You should define a dedicated epsilon value yourself as your project needed.

You are comparing the user-input number to zero, and I saw MaxLength="5" in your XML. Your epsilon value should be 0.0001 since it's smaller than 0.001 which is the minimal non-zero input value of your <entry> control.

Upvotes: 1

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34180

You may use RegularExpressions:

bool isZero = Regex.Matches(input,"[0,]");

Or

bool isZero = int.Parse(input.Replace(",","") == 0;

Instead of trying to forcing it to be a valid double number by removing extra commas, non-numeric chars ,... try to validate it:

  public static bool IsValidDouble(this string s)
  {
      double d = 0;
      double.TryParse(s, out d);
      return d != 0; //will be false if result is 0 
      //return d > 0; if you don't want negativer values 
  }

Upvotes: 2

Related Questions