This question already has answers here:
How to round an integer up or down to the nearest 10 using Javascript
(4 answers)
Closed 6 years ago.
I need to round a number to the nearest multiple of 27 with JavaScript. It would be better if I could always round UP to the number, but it would also be useful to know how to round to the closest multiple of 27 (whether up or down). It doesn't have to be vanilla JavaScript it could be jQuery too.
Divide the number by 27, round (or .ceil to round it up) the result and multiply by 27:
var x = 28;
console.log('round', Math.round(x/27) * 27);
console.log('ceil', Math.ceil(x/27) * 27);
var y = 47;
console.log('round', Math.ceil(y/27) * 27);
console.log('ceil', Math.round(y/27) * 27);
Related
This question already has answers here:
Is there a way to round numbers into a reader friendly format? (e.g. $1.1k) [closed]
(2 answers)
1000000 to 1M and 1000 to 1K and so on in JS [duplicate]
(1 answer)
Closed 5 years ago.
I want to trim my 5 or more than 5 digit number 12345 to
12.34k
to fit it in my small box. Is there any util to use?
you can divide it by 1000 and then specify the number of digits you want using toFixed()
let x = 12345;
x = x/1000;
x = x.toFixed(2);
x = x + "k";
console.log(x)
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
I have a JavaScript function that returns the result of multiplications using decimal values (four digits after dot).
But, in some conditions, the result is a mess like this:
3.9050 * 9 = 35.144999999999996.
What should I do to normalize those results?
Use .toFixed(numberOfDecsYouWant)
var num = 3.9050 * 9;
console.log(num); //35.144999999999996
console.log(num.toFixed(4)) //35.1450
This question already has answers here:
How to deal with big numbers in javascript [duplicate]
(3 answers)
Closed 8 years ago.
If I use the windows calculator to calculate
(1.75 + 3/1.75)/2 it yields to =
1,7321428571428571428571428571429
If I do the same with javascript it yields to
1.732142857142857206298458550009
So at position 22 after the decimal point the result becomes incorrect ...142857... vs. ...206298...
var a = 1.75;
var res = (a+3/a)/2;
console.log(res.toFixed(30));
How can I make my division precise for 31 digits after the decimal comma?
Javascript can't do that "per se", since its double variables have a limited precision. You'll need to use an external library to handle operations with big precision numbers, like this one: Javascript Bignum
This question already has answers here:
Format number to always show 2 decimal places
(37 answers)
Closed 8 years ago.
How can I make a flaoting point number always have two decimal places, even when one is a zero?
E.g var postageCost = 3.2 but I want to display it as 3.20 - is this something that must be added as a string or is it possible to actually change the number?
From Here, you can use
parseFloat(Math.round(postageCost * 100) / 100).toFixed(2);
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
javascript - ceiling of a dollar amount
I have a number like this: 360.654444444447
I want this to round up to 360.66
How do i do it? The amount of 4's between the 5 and 7 is unknown.
EDIT: The key issue here is that when the decimals after the 2nd would round to 5, it should round up. (ie: 360.654447 can be rounded to 360.655 - and that should round to 360.66
It's similar to the PHP_HALF_ROUND_MODE thing.
Use the ceil method, not the round method as others have suggested.
var number = 360.654444444447;
var result = Math.ceil(number * 100) / 100;