Javascript parseFloat issue [duplicate] - javascript

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
I am facing a new problem. I have the below use case.
var a = parseFloat(10); // Returns 10
var b = parseFloat(1.62); // Returns 1.62
var c = a + b; // Returns 11.620000000000001
I want the c to return 11.62. What is the problem here?

var a = parseFloat(10); // Returns 10
var b = parseFloat(1.62); // Returns 1.62
var c = a + b; // Returns 11.620000000000001
console.log(c.toFixed(2))
Output: 11.62
console.log(c.toPrecision(4))
Output: 11.62

var a = parseFloat(10); // Returns 10
var b = parseFloat(1.62); // Returns 1.62
var c = a + b; // Returns 11.620000000000001
console.log(c.toPrecision(4));
Output: 11.62

Related

JavaScript function default parameters [duplicate]

This question already has answers here:
Skip arguments in a JavaScript function
(9 answers)
Passing values to function with multiple default parameters
(1 answer)
Default parameter of a JavaScript function is not working when intermittent value is missing
(1 answer)
Pass only the second argument in javascript
(4 answers)
Omit/Skip a parameter with default value while calling a function in JS
(2 answers)
Closed last year.
const add = (a = 1, b = 1, c = 1) => a + b + c
add(4, , 2)
Throws Uncaught SyntaxError, unexpected token ','
How do I call the function so b defaults to the value 1
Just take undefined as value.
const add = (a = 1, b = 1, c = 1) => a + b + c
console.log(add(4, undefined, 2));
Pass undefined as value
Check this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters#passing_undefined_vs._other_falsy_values
const add = (a = 1, b = 1, c = 1) => a + b + c
console.log(add(4, undefined, 2))

Adding commas to number toFixed toLocaleString [duplicate]

This question already has answers here:
JavaScript: Format whole numbers using toLocaleString()
(4 answers)
Closed 1 year ago.
I am creating a calculator and need guidance on adding thousand separators to a whole number.
//logic
var portVal = 100000000;
var rateUSD = 1.3654;
var sustPortBase = 22;
var avgPortBase = 66;
var sustPortCarb = (portVal*rateUSD/1000000)*sustPortBase;
var avgPortCarb = (portVal*rateUSD/1000000)*avgPortBase;
var diff = avgPortCarb - sustPortCarb;
var nbHomes = diff * 0.115;
var nbCars = diff * 0.216;
var nbTrees = diff * 16.5;
var nbPlanes = diff * 0.833359;
var avgPortCarbRound = avgPortCarb.toFixed();
var sustPortCarbRound = sustPortCarb.toFixed();
console.log("Cars:"+(nbCars).toFixed());
console.log("Cars Decimals:"+(nbCars).toLocaleString('en'))
Outputs:
Cars:1298
Cars Decimals:1,297.676
I would like to keep only thousand seperators but nothing after the (dot.) i.e. 1,297
https://jsfiddle.net/ge5oj2xk/1/
Try using Math.floor function.
console.log("Cars Decimals:"+Math.floor(nbCars).toLocaleString('en'))

when taken 5 root of big number gives wrong answere in js [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 2 years ago.
When I take the 5 root of a big number, js gives me a wrong answer
var pow = 5;
var A = 133;
var B = 110;
var C = 84;
var D = 27;
var E;
E = Math.pow((Math.pow(A, pow) + Math.pow(B, pow) + Math.pow(C, pow) + Math.pow(D, pow)), 1/pow);
console.log(E)
This should give 144 but gives 144.00000000000003
5 root of 61917364224 = 144

Ternary operator not working properly in JS [duplicate]

This question already has answers here:
Operator precedence with JavaScript's ternary operator
(7 answers)
Ternary operation JS not working
(3 answers)
Closed 3 years ago.
I am running the following code and getting unexpected results:
var a = 1, b =2 ,c = 3;
console.log(5*a+ b>0?b:c);
Expected result was : 7 but getting 2.
Your code has the right concept, but wrong execution. The ternary is doing its job properly.
At the moment, your code is executing like this:
const a = 1
const b = 2
const c = 3
// This will evaluate to true, since 5 * 1 + 2 = 7, and 7 is greater than 0
if (5 * a + b > 0) {
// So return b
console.log(b)
} else {
console.log(c)
}
You should use brackets to separate the ternary:
const a = 1
const b = 2
const c = 3
console.log(5 * a + (b > 0 ? b : c));

Adding two integer JavaScript [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
I am trying to add two numbers in JavaScript, but the result is inexact. This my example:
var x = 11.12, y = 11.07;
console.log(x + y); // the result is 22.189999999999998 but the real result is 22.19
Any solution for this?
This is what you need to do:
var x = 11.12, y = 11.07;
var result = (parseFloat(x) + parseFloat(y)).toFixed(2);
console.log( result );

Categories

Resources