0

Super simple - I'm running a useEffect function in React. I need to do a simple calculation, but I keep getting the wrong answer.

Example, I get 10 as an answer instead of the expected 8,960...

I thought it might be a string and not an Int at first, but it made no difference.

const price = 5.58;
console.log(price); // returns 5.58
const money = 50.00;
console.log(money); // returns 50
const dev = parseInt(money) / parseInt(price);
console.log(dev) // returns 10 ???.

I expected it const dev to return 8,960573476702509 but it returned 10

devserkan
  • 16,870
  • 4
  • 31
  • 47
Ronald Langeveld
  • 694
  • 1
  • 8
  • 23

3 Answers3

3

You're doing division between two integers, as parseInt(50.00) will give 50 and parseInt(5.58) gives 5, so your calculation will be doing 50/5 which is equal to 10. To perform math with your floating-point numbers, there is no need to parse them as they're already floats:

const price = 5.58; // already a float
console.log(price); // returns 5.58
const money = 50.00; // already a float
console.log(money); // returns 50
const dev = money / price;
console.log(dev) // 8.960573476702509
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • 1
    Yes, that's better. The problem is that in plain maths we often jump between number sets - if you write down `11 / 2` on a piece of paper and give it to somebody, they will not hesitate to give you an answer of `5.5` or `5 1/2`. But that's going from an integer to a real number or rational number. In JS all you have is floats so `5` is not actually an integer but in, say, Java or C `int a = 11; int b = 2; a / b` will result in an integer because `int / int -> int` – VLAZ Aug 27 '19 at 06:59
  • @VLAZ yeah, for some reason I had it in my head that integer division was division between two integers, but now that you've clarified it makes total sense. Cheers :) – Nick Parsons Aug 27 '19 at 07:05
1

You need to use parseFloat as you are dealing with decimal number

const price = 5.58;
console.log(price); 
const money = 50.00;
console.log(money); 
const dev = parseFloat(money) / parseFloat(price);
console.log(dev)
  • 2
    Those numbers are already *numbers*. Using `parseFloat` makes no sense. – VLAZ Aug 27 '19 at 06:47
  • Makes sense depending of what you want / expect to get. If you want to get `10` it makes no sense to use parseFloat, if you want to get 8.96... you need to use parseFloat – ProblemsEverywhere Aug 27 '19 at 07:03
  • 1
    No, it still doesn't make sense. `parseFloat(5.58)` will not give you *more* of a float, since you are already giving it a float. At best, you get the same value back. However, since *parsing* is done on strings, what actually happens is that the value would be turned from numeric to a string and then transformed to a numeric again. This *might* lead subtle bugs with some values, if the string representation differs slightly. – VLAZ Aug 27 '19 at 07:09
  • Oh! I understand! Thanks a lot! @VLAZ – ProblemsEverywhere Aug 27 '19 at 07:27
1

const price = 5.58;
console.log(price); // returns 5.58
const money = 50.00;
console.log(money); // returns 50
const dev = money / price;
console.log(dev) // returns 10 ???.
  • 2
    The two values are already numeric - there is no reason to run then through `parseFloat`. – VLAZ Aug 27 '19 at 06:50