-1

I know JavaScript has a notorious problem with math that can cause math problems to return wrong with wonky decimal numbers. But how does one do a simple addition and subtraction in JS and not have a wonky result? And the numbers for the addition and subtraction are not fixed numbers and relay on variables. For example...

addition

var a = 3.94
var b = 0.5
var r = a + b
console.log(r) //4.4399999999999995

subtract

var a = 4.22
var b = 0.5
var r = a - b
console.log(r) //3.7199999999999998

I am aware of the math floating point issue in JS but I am looking for a simple solution to not have this issue occur when doing some addition and subtraction.

Is floating point math broken?

Hama21
  • 19
  • 2
  • 1
    https://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript – epascarello Oct 12 '18 at 18:48
  • I can't figure out your problem, JS and many other languages as well as the CPU is based on IEEE 763 floating point arithmetics, so the 3.94 in decimal has to be translated into this binary format, see http://www.binaryconvert.com/convert_float.html and this is not exactly the same in decimal. – Tom Kuschel Oct 12 '18 at 18:52
  • 2
    use toFixed. var r = Number((a + b).toFixed(2)) – Stakvino Oct 12 '18 at 18:53
  • 1
    You _cited_ another question which I think [explains the issue pretty well](https://stackoverflow.com/a/588014/17300). Is there something you don't understand about the answers there? – Stephen P Oct 12 '18 at 18:56
  • @Hama21 It would be helpful for us if you could re-ask the question in your own words. There appear to be some High-Quality Answers in the first post.I'm going to downvote this for now. But if you edit to Inquire a Detail i will revisit – Luke Hammer Oct 12 '18 at 19:11
  • 1
    @stephen he did not ask for the "why?" but for a "how to solve", not sure if it is helpful to point / dupe vote for an answer that did not help the OP. – Jonas Wilms Oct 12 '18 at 20:16
  • 1
    @wizardHammer those are "his words" ? Not sure how he could improve his question, it is actually quite on-point. Yes, there are some high-quality answers out there but that is not a reason to downvote, rather a reason to dupe-vote. – Jonas Wilms Oct 12 '18 at 20:17
  • @StephenP & WizardHammer: I understand the problem and what causes this but I want to solve the issue when doing addition and subtraction. In the related articles about this I cannot find an answer to making addition (a+b) or subtraction (a-b) work. – Hama21 Oct 12 '18 at 21:40
  • Hama21 - I recognize the validity of @Jonas comment; that my link is about the explanation and not about the mitigation — other linked "possible duplicate" comments _do_ address mitigation, such as epascarello's link which points to using a dedicated library or using `.toPrecision(...)` ... It _really does_ come down to your usage and goals. Early in my career I worked in the financial sector, and we did calculations in _integers_ representing 1/100th of a cent, never _ever_ in floating point. It's not that "JavaScript has a notorious problem with math" — it's limitations in _any_ language. – Stephen P Oct 13 '18 at 00:25
  • @Hama21 Your Comment makes sense. As I read your original Question. The question that it appears that you are asking is "Is floating point math broken?" I don't think that is what you are attempting to ask. That is why i"m suggesting a Rewite of the Question. – Luke Hammer Oct 14 '18 at 22:53
  • @JonasWilms I was referring to the question "Is floating point math broken?" that now appears not to be the question being asked. I see the second question in a paragraft above " But how does one do a simple addition and subtraction in JS and not have a wonky result?" i would suggest this is the main question and if so Make that clear. Here are my suggestions for Improving – Luke Hammer Oct 14 '18 at 22:57
  • Here are my suggestions for Improving. 1. move the question to the bottom. 2. Reask in the positive. 3. Set the Question apart from the backstory and code. – Luke Hammer Oct 14 '18 at 23:04

1 Answers1

1

Thanks to @Stakvino suggestion I think I figured it out.

Let me know what you guys think.

addition

var a = 3.94
var b = 0.5

var fixed = String(a)

if (fixed.indexOf('.') === -1) {
    fixed = 0;
}

else {
    fixed = fixed.split('.')
    fixed = fixed[1].length;
}



var r = (a + b).toFixed(fixed)
console.log(r)

subtraction

var a = 4.22
var b = 0.5

var fixed = String(a)

if (fixed.indexOf('.') === -1) {
    fixed = 0;
}

else {
    fixed = fixed.split('.')
    fixed = fixed[1].length;
}



var r = (a - b).toFixed(fixed)
console.log(r)
Hama21
  • 19
  • 2