javascript round up from 3rd decimal [duplicate] - javascript

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;

Related

How to convert "23.99999999999999" (repeating decimal with a repetend of 9) to "24" (correct integer) in JavaScript? [duplicate]

This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 24 days ago.
When I execute the JavaScript code 8/(3-8/3), the result is displayed as 23.99999999999999, but I want to fix it to 24.
In order not to round numbers when they are not repeating decimals or when their repetend is not 9 (e.g. do not round 21.835 to 22; do not round 2.979797979797 to 3), how should I solve this problem?
There doesn't seem to be a clear way to resolve this issue without using some sort of rounding function, but there are alternative ways to write this specific equation, according to MathsIsFun
E.g.
8/(1/3)

How to get the first four decimal numbers in Javascript using math.ceil? [duplicate]

This question already has answers here:
How to round up a number in Javascript?
(10 answers)
Closed 4 years ago.
Currently have the following javascript code:
div.html( Drupal.t("Date")+": "+d.date+"</br>"+Drupal.t("Value")+": " + (d.value))
Value that i'm getting is 0.1651859999999985 . I wanna round it off to the first four decimal numbers. Any help? I tried Math.ceil(d.value*10)/10) but it was only getting the first decimal :( Any help? I wanna get 0.1652
Should be Math.ceil(d.value*10000)/10000
example:
console.log(Math.ceil(0.1683123*10000)/10000)
console.log(Math.ceil(823.124148336763*10000)/10000) // round up to the ceiling
console.log(Math.floor(823.124148336763*10000)/10000) // round down to the floor
console.log(Math.round(823.124148336763*10000)/10000) // below .5 -> down, above .5 -> up

How to round to a multiple of a specific number JavaScript? [duplicate]

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);

Simple Calculation - JavaScript [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
I am performing a basic calculation using javascript and when I use this combination of number it won't calculate correctly:
alert((40071.13 + 6028.91) - 46100.04);
It should calculate to 0 but it doesn't. All other number combinations work for me.
Help!
This is a rounding issue. Try this to round to 2 decimal places
var num = (40071.13 + 6028.91) - 46100.04;
alert(num.toFixed(2));
See it in action on jsFiddle

Show 0 value in floating point values, for currency [duplicate]

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);

Categories