21

I'm doing simple math in JavaScript using variables to represent the numbers. Here is an example of my code:

var ones = 0;
var fives = 0;
function results (){
    _fives = (fives * 5);
    var res = (_fives + ones);
    document.innerHTML = res;
}

This isn't the full code but basically I'm having the user enter the amount of bills and coins from 1 cent coins up to $100 bills. The code multiplies the amount of bills to the amount the bill is worth. This is no problem works just fine... For some reason on some of my results it shows a decimal like 1.899999999997 not sure how this is happening.

Is there a way to change this so it round to the nearest hundredth of a decimal?

For example instead of it showing 1.89999999997 it would just show 1.90 in reality this isn't a big issue. This is a personal thing that I can just round it myself however it would be nice to learn how to do this for future reference.

4
  • 2
    What have you tried? There's this tempting Math.round function, for instance. Commented Feb 19, 2013 at 22:19
  • Look for "float point arithmetic". This isn't a JavaScript "issue" only. Commented Feb 19, 2013 at 22:23
  • 3
    _fives is now a global variable. Commented Feb 19, 2013 at 22:24
  • possible duplicate of Dealing with accuracy problems in floating-point numbers Commented Feb 20, 2013 at 6:15

8 Answers 8

31

UPDATE: MDN actually has a great example of decimal rounding that avoids floating point inaccuracies. Their method can be modified to always round up, based on the OP.


ORIGINAL (SOMEWHAT INCORRECT) ANSWER

//to round to n decimal places
function round(num, places) {
    var multiplier = Math.pow(10, places);
    return Math.round(num * multiplier) / multiplier;
}

EDIT: I didn't read the question completely. Since we're talking currency, we probably want to round it up:

//to round up to two decimal places
function money_round(num) {
    return Math.ceil(num * 100) / 100;
}
Sign up to request clarification or add additional context in comments.

7 Comments

How does this work? Should I replace anything or is this exactly what I should put into my code to round it up?
Ah yes...it doesn't round up. Edit incoming
Now to answer your question. Javascript's Math.round method rounds to the nearest integer. By multiplying your original number by a multiple of ten (one for each decimal you want to preserve) before rounding then dividing by that same value, you preserve the decimals you need. This doesn't solve the problem with floating point numbers, but it will at least allow you to print nice money quantities.
Seems to work now. I had to play with the code a bit but now it's perfect =] thanks a lot!
This only works approximately. It doesn't and can't round to an exact number of decimal places, because floating-point doesn't have decimal places, it has binary places, and they are incommensurables with decimal places.
|
13

This is what I personally decided to use in one of the apps I'm working on!

const roundToHundredth = (value) => {
  return Number(value.toFixed(2));
};

console.log(roundToHundredth(1.98723232))
console.log(roundToHundredth(3.14159265359))
console.log(roundToHundredth(2.67528))

1 Comment

That's going to give you a string though, so if you need to work with a number you may have to parse it back.
1

I've actually been dealing with this as I've been working to implement a solution for rounding for a vectors project I am working on. I'm actually surprised this older (2011) javascripter.net resource hasn't made it, particularly as it is using Math.round( // ... mentioned previously.

I've added a bit to it, also make use of .toFixed() (though this will lop off any trailing zeroes, which I am not too worried about, at this stage, personally):


const round = (number, places) => {
   let rounder = '1';

   for (let i = 0; i < places; i++) {
       rounder += '0';
   }

   return (Math.round(number * rounder) / rounder).toFixed(places);
};

I'm sure there is some issue with the above in terms of efficiency or redundancy; but my point is that Math.round() still does what you need it to. Might need to throw a conversion in the above, after the iteration.

It's also a heck of a lot easier to read than some of the other proposed solutions.

Comments

0

I know that this is likely a day late and a dollar short, but I was also looking for something like this, and this question led me to look into the Math.round functionality

I come up with this which successfully rounds a given value to the nearest 5th, 100th. So 0.07 becomes 0.05. 23.45 becomes 23.45. 1006.32 becomes 1006.30.

<script>
function rounder() {
var exampleInput = $("#inputId").val();
$("#output").html(((Math.round(((Math.round(exampleInput*100)/5)*5)/5)*5)/100).toFixed(2));
}
</script>

Being a novice, I am certain that there is a way to make this code more efficient.

1 Comment

A for effort but I think you can do better than this now
0

Rounding up tool for large decimals. Can be adjust to handle different decimals sizes by assigning 'decimal' a different value.

const roundToWholeNumber = (num, decimal = 100000) => {

    const _roundingHelper = (val, decimalPlace) => {

        return Math.round(val * decimalPlace) / decimalPlace;

    }

    while(decimal > 1){
        num = _roundingHelper(num, decimal)
        decimal = decimal / 10
    }

    return Math.round(num)   
}
roundToWholeNumber(5.44444555)

Comments

0

This is known problem of JavaScript when you doing something like:

0.1 + 0.5 = 0.600000000001 or 0.599999999998

The solution for this problem is to use one of JavasSript library with fixed precision and rounding modes. You can try scam link. Here is code example:

// Initialization
var bn = new BigNumbers({
    precision: 20, // fixed precision 20 digits after decimal separator 
    roundingMode: BigNumbers.RoundingMode.HALF_UP // rounding mode is Half Up
});

var number1 = bn.of('15');
var number2 = bn.of('12');


var divideResult = number1.divide(number2); // result "1.25"
var multiplyResult = divideResult.multiply(1/2); // result "0.625"
var roundingToTwoDecimalResult = multiplyResult.toPrecision(2); // result "0.63"

( external links provided removed after becoming broken or hacked by scammers - Oct 2020)

Comments

0

Try using a function to run the rounding system:

          function roundNumber(num) {
             return Math.round(num * 100) / 100;
            }
           var x = roundNumber(y); //round your variables using the roundNumber function
          

Comments

0

More straightforward solution with toFixed and unary plus (+):

let twoThirds = 2/3; // number: 0.666666...
let rounded = +twoThirds.toFixed(2); // number: 0.67
console.log({twoThirds,rounded});

This is a common format for money (ex: $100.67) to have exactly two digits. Therefore we use toFixed rather than toPrecision

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.