Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round off to the nearest 10 000 Javascript

I have two variables one for decimal numbers and one for integer. Now, i add the decimal numbers with each other and the integers with each other then multiply the sum of decimal numbers and the sum of the integers. My problem now is i want to round them off to the nearest 10 000

So, if 2,54 * 40 000 = 101600 i want my div to display 110 000. Is this possible? I never know what the sum of the decimal numbers or the integers are, i just use two variables

like image 856
simsalabim33 Avatar asked Mar 03 '14 14:03

simsalabim33


People also ask

How do you round numbers to the nearest 10000?

To round a number to the nearest 10,000, check the thousands digit to decide whether to round up or round down. If the thousands are 5000 or more, round up. If they are 4999 or less, round down. 45,000 to the nearest 10,000 is 50,000.

How do you round thousands in JavaScript?

How do you round to the nearest thousandth in Javascript? round(1000*X)/1000; // round X to thousandths To convert a number to a string that represents your number with exactly n decimal places, use the toFixed method.

How do you round to the nearest 10 in JavaScript?

To round a number up to the nearest 10, call the Math. ceil() function, passing it the number divided by 10 as a parameter and then multiply the result by 10 , e.g. Math. ceil(num / 10) * 10 .

How do you round off in JavaScript?

JavaScript Math.round() The Math.round() method rounds a number to the nearest integer. 2.49 will be rounded down (2), and 2.5 will be rounded up (3).


3 Answers

Math.round(101600 / 10000) * 10000 // --> 100000
Math.floor(101600 / 10000) * 10000 // --> 100000
Math.ceil(101600 / 10000) * 10000 // --> 110000
like image 55
Sebastien C. Avatar answered Oct 18 '22 03:10

Sebastien C.


var round = 10000;
var result = round * Math.round(answer / round);
like image 31
devqon Avatar answered Oct 18 '22 04:10

devqon


Had to do this recently. Here is a function I wrote to do this automatically.

function getRoundedZeros(value, up){
        var roundto = '1';

        for(i = 1;i < value.toString().length; i++){
            roundto = roundto.concat('0');
        }

        roundto = parseInt(roundto);

        if(up === true){
            return Math.ceil(value / roundto) * roundto;
        }else{
            return Math.floor(value / roundto) * roundto;
        }
    }

    rounded = getRoundedZeros(UNrounded, true);

I hope it helps someone :)

like image 1
Bullyen Avatar answered Oct 18 '22 02:10

Bullyen