AngularM
AngularM

Reputation: 16638

I want to compare 2 decimals to see which one is greater

I want to compare 2 decimals to see which one is greater.

This doesn't seem to be working for decimals and only works for integers (e.g. 1 > 2) and doesn't work for floats (1.67 > 1.98).

This is my example that doesn't work:

this.testOrder = (valA, valB): boolean => {
    const radix = 10;
    return parseInt(valA, radix) > parseInt(valB, radix);
};

Upvotes: 0

Views: 67

Answers (3)

Yosvel Quintero
Yosvel Quintero

Reputation: 19080

You can use Unary plus (+):

this.testOrder = (valA, valB): boolean => {
  return +valA > +valB;
};

Example:

var a = +'1.67',
    b = +'1.98';

console.log('a:', a);
console.log('b:', b);

console.log('boolean result (a > b):', a > b);

Upvotes: 0

Vikrant
Vikrant

Reputation: 5036

Try below solution:

this.testOrder = (valA, valB): boolean => {
    const radix = 10;
    return parseFloat(valA, radix) > parseFloat(valB, radix);
};

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386720

Use parseFloat instead of parseInt.

parseInt takes only the integer part of a string.

Upvotes: 2

Related Questions