JSFiddle - React, Tailwind, and code Playground

by Ilmv

JavaScript

//
// READ ME!
//
// Lots answer to my problem, but is there a definitive "better" solution, or are they all just as good as each other?
//


//********************
// MY ORIGINAL PROBLEM

var a = -257302.88,
    b = -257281.42;

// this calculation should return −21.46
alert(a - b); // WTF!! 


 //**************
// @BenjaminReid

var a = -257302.88,
    b = -257281.42;

alert(Math.round((a - b)*100)/100);


 //********
// @mheaps

var a = -257302.88,
    b = -257281.42;

a *= 1000000000;
b *= 1000000000;
var c = a - b;
alert(c / 1000000000);


 //***********
// @jccanizal

var a = -257302.88,
    b = -257281.42;

var c = a - b;
c = c.toFixed(2); 
alert(c);


// They ALL provide the same solution, but do either have flaws?