Round number up in JavaScript [duplicate] - javascript

This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 8 years ago.
I have the following number
0.135
I'd like to round it to 2 decimal places, I'm using...
(newCostDiff/2).toFixed(2)
only this returns
0.13
Can anyvody advise me on how to do this?

What's need of jquery
var newCostDiff = 0.135;
Math.round(newCostDiff *100)/100; //returns 0.14

You should first multiply the result by 10^(number of decimals), then round the number, than divide this number by 10^(number of decimals).
In this case:
Math.round((newCostDiff/2)*100) / 100;

Related

Random Decimals Behind A Number [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
Every time I do something like + 1.6 or something that's a decimal, I get a lot of random numbers spammed behind the result. how to avoid?
var test = 1.2345;
console.log(test.toFixed(2))
// It will trim the result to the specified decimal places.
// |----> output will be 1.23 in thid case... It will keep upto 2 decimal places

Cubejs Type declaration in measures for round up decimal values [duplicate]

This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 3 years ago.
is there any method to roundup the decimal values for example in measures i am defining BaseAmount:{
sql:'BaseAmount',
type:sum
},
I am getting sum of all baseAmount value like this 693.3399999999993 because here I mentioned type as sum I need to round up these values as 693.3 for that what type I need to declare
You can use the Math.round function in js. See the below snippet.
let sum = 693.3399999999993;
let rounded = Math.round( sum * 10 ) / 10;
console.log(rounded)

How to add fraction and whole number number in javascript? [duplicate]

This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 5 years ago.
for example in javascript addition of 1 + 0.59 is 1.5899999999999999 but I want output of 1.59 which looks right to me.
Use .toFixed() to round your number.
alert((1+0.58999999999).toFixed(2));

javascript math result lesser than 0 gets 0.0000000000 [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
Why does javascript returns so many zeros and not just 0.24
And how can i disable javascript to do this. Because when im using a calculator i never get the result 0.24000000000002
var sum = (0.0001 * 2400);
result 0.2400000000002
The reason for this is that your sum is a float which are known to not be very precise. This is a limitation of float values.
To fix this you need to round the decimals by either Math.round or .toFixed.
javascript always do that but you can make it show only 2 digits after the dot.
var sum = (0.0001 * 2400);
alert(sum.toFixed(2));

strange Javascript Float division result [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
var coeff='0.1';
var amount='12.2';
var res = Math.floor(parseFloat(amount) / parseFloat(coeff));
console.log(res);
Why the result of this is 121 (I was expecting 122)?
EDIT: my question was ambiguous: no trouble with the floor function. I was just wondering why 12.2 / 0.1 is not equal to 122.
the result is 121.99999999999999
from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
The Math.floor() function returns the largest integer less than or equal to a given number.
so it cuts off the .99999. you might want to use
Math.round(parseFloat(amount) / parseFloat(coeff));
instead

Categories

Resources