0

In Sencha Touch I use number fields to get some values:

 var heightInFt = Ext.getCmp('HeightFt').getValue();
 var heightInIn = Ext.getCmp('Height').getValue();

 var totalHeightInFt = (heightInFt * 12) + heightInIn;

 bmrValue = 655 + (4.35 * weightInLb) + (4.7 * totalHeightInFt) - (4.7 * age);

 bmrValue = Math.round(bmrValue * 100) / 100;

Then when I multiply

 calories = bmrValue * 1.55;

I get this result: 1544.9 * 1.55 = 2394.5950000000003

What am I doing wrong? What should I do in this situation?

Zingam
  • 4,498
  • 6
  • 28
  • 48
  • 9
    [Is JavaScript's Floating-Point Math Broken?](http://stackoverflow.com/questions/588004/is-javascripts-floating-point-math-broken) –  Jul 30 '13 at 13:31
  • 1
    Floating point math does this kind of thing. Live with it. Use `Math.round()` and `Math.toFixed()` to drop the unwanted precision. – Spudley Jul 30 '13 at 13:33
  • what result did you expect? – Max Adamek Jul 30 '13 at 13:33
  • So I basically should always round the results??? @CrazyTrain Thank you for the link! – Zingam Jul 30 '13 at 14:10
  • 1
    Its not a global JavaScript problem. It won't apply to Internet Explorer, but happends in Chrome and Firefox. The explanation would be: Because fractional values cannot be perfectly represented by floating point numbers. – Dinkheller Aug 01 '13 at 11:10

0 Answers0