Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript math, round to two decimal places [duplicate]

I have the following JavaScript syntax:

var discount = Math.round(100 - (price / listprice) * 100); 

This rounds up to the whole number. How can I return the result with two decimal places?

like image 782
Smudger Avatar asked Apr 02 '13 11:04

Smudger


People also ask

How do you round a double value to 2 decimal places in JavaScript?

Use the toFixed() method to round a number to 2 decimal places, e.g. const result = num. toFixed(2) . The toFixed method will round and format the number to 2 decimal places.

What is the use of toFixed 2 in JavaScript?

The toFixed() method converts a number to a string. The toFixed() method rounds the string to a specified number of decimals.

How do you round a double to 2 places?

DecimalFormat(“0.00”) We can use DecimalFormat("0.00") to ensure the number always round to 2 decimal places. For DecimalFormat , the default rounding mode is RoundingMode.

How do you round a double value in JavaScript?

Math.round() is a function used to return the value of a floating-point number rounded to the nearest whole integer. Depending on the supplied float/double, Math. round() either rounds up or down.


2 Answers

NOTE - See Edit 4 if 3 digit precision is important

var discount = (price / listprice).toFixed(2); 

toFixed will round up or down for you depending on the values beyond 2 decimals.

Example: http://jsfiddle.net/calder12/tv9HY/

Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

Edit - As mentioned by others this converts the result to a string. To avoid this:

var discount = +((price / listprice).toFixed(2)); 

Edit 2- As also mentioned in the comments this function fails in some precision, in the case of 1.005 for example it will return 1.00 instead of 1.01. If accuracy to this degree is important I've found this answer: https://stackoverflow.com/a/32605063/1726511 Which seems to work well with all the tests I've tried.

There is one minor modification required though, the function in the answer linked above returns whole numbers when it rounds to one, so for example 99.004 will return 99 instead of 99.00 which isn't ideal for displaying prices.

Edit 3 - Seems having the toFixed on the actual return was STILL screwing up some numbers, this final edit appears to work. Geez so many reworks!

var discount = roundTo((price / listprice), 2);  function roundTo(n, digits) {   if (digits === undefined) {     digits = 0;   }    var multiplicator = Math.pow(10, digits);   n = parseFloat((n * multiplicator).toFixed(11));   var test =(Math.round(n) / multiplicator);   return +(test.toFixed(digits)); } 

See Fiddle example here: https://jsfiddle.net/calder12/3Lbhfy5s/

Edit 4 - You guys are killing me. Edit 3 fails on negative numbers, without digging into why it's just easier to deal with turning a negative number positive before doing the rounding, then turning it back before returning the result.

function roundTo(n, digits) {     var negative = false;     if (digits === undefined) {         digits = 0;     }     if (n < 0) {         negative = true;         n = n * -1;     }     var multiplicator = Math.pow(10, digits);     n = parseFloat((n * multiplicator).toFixed(11));     n = (Math.round(n) / multiplicator).toFixed(digits);     if (negative) {         n = (n * -1).toFixed(digits);     }     return n; } 

Fiddle: https://jsfiddle.net/3Lbhfy5s/79/

like image 93
Rick Calder Avatar answered Sep 28 '22 22:09

Rick Calder


If you use a unary plus to convert a string to a number as documented on MDN.

For example:+discount.toFixed(2)

like image 28
John Gietzen Avatar answered Sep 29 '22 00:09

John Gietzen