Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to round in javascript like PHP do

I'm trying to round a float number in Javascript in the same way that I do it in PHP; but I can not make both languages ​​round in the same way the following number:6.404999999999999

When I use PHP round I get: 6.41, but when I trying to round with Javascript I always get 6.40

INFO: The correct answer is https://stackoverflow.com/a/54721202/4359029

My Javascript attempts:

Attempt #1:

module.exports = function round (value, precision, mode) {
  var m, f, isHalf, sgn // helper variables
  // making sure precision is integer
  precision |= 0
  m = Math.pow(10, precision)
  value *= m
  // sign of the number
  sgn = (value > 0) | -(value < 0)
  isHalf = value % 1 === 0.5 * sgn
  f = Math.floor(value)

  if (isHalf) {
    switch (mode) {
      case 'PHP_ROUND_HALF_DOWN':
      // rounds .5 toward zero
        value = f + (sgn < 0)
        break
      case 'PHP_ROUND_HALF_EVEN':
      // rouds .5 towards the next even integer
        value = f + (f % 2 * sgn)
        break
      case 'PHP_ROUND_HALF_ODD':
      // rounds .5 towards the next odd integer
        value = f + !(f % 2)
        break
      default:
      // rounds .5 away from zero
        value = f + (sgn > 0)
    }
  }

  return (isHalf ? value : Math.round(value)) / m
}

Extracted from: http://locutus.io/php/math/round/

Attempt #2:

round(decimal: number, decimalPoints: number): number{
    let roundedValue = Math.round(decimal * Math.pow(10, decimalPoints)) / Math.pow(10, decimalPoints);

    console.log(`Rounded ${decimal} to ${roundedValue}`);
    return roundedValue;
}

Extracted from: https://stackoverflow.com/a/50918962/4359029


I tried with other solutions... but without success.

Could someone tell me how to get the rounding of Javascript to act like PHP's?

Could you tell me why they work in different ways in the case I explain?

like image 610
tomloprod Avatar asked Nov 23 '18 16:11

tomloprod


People also ask

How do I round 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 does round do in PHP?

Definition and Usage. The round() function rounds a floating-point number. Tip: To round a number UP to the nearest integer, look at the ceil() function. Tip: To round a number DOWN to the nearest integer, look at the floor() function.

How do you round decimals in JavaScript?

The most common solutions for rounding to a decimal place is to either use Number. prototype. toFixed() , or multiply the float by some power of 10 in order to leverage Math. round() .


4 Answers

I think the best answer is the next:

function roundLikePHP(num, dec){
  var num_sign = num >= 0 ? 1 : -1;
  return parseFloat((Math.round((num * Math.pow(10, dec)) + (num_sign * 0.0001)) / Math.pow(10, dec)).toFixed(dec));
}

I tried the other solutions with 1.015 and doesn't work as expected.

This solution works well, like PHP round, with all the numbers I tried.

like image 134
tomloprod Avatar answered Nov 01 '22 14:11

tomloprod


You might try

console.log(Number(n.toFixed(3)).toFixed(2));

eg.

var n = 6.404999999999999;
console.log(n.toFixed(2)); //=> 6.40
console.log(n.toFixed(3)); //=> 6.405
like image 40
Chameera Avatar answered Nov 01 '22 13:11

Chameera


You could do it like this:

function roundToXDigits(value, digits)
{
    if (!digits) {
        digits = 2;
    }

    value = value * Math.pow(10, digits);
    value = Math.round(value);
    value = value / Math.pow(10, digits);

    return value
}

var num = roundToXDigits(6.404999999999999, 3); //creates 6.405

console.log(roundToXDigits(num, 2)); //creates 6.41

The problem is, JavaScript technically isn't wrong. 6.4049 -> becomes 6.405 when you round (which, to 2dp is 6.40) which is why it's not working as expected. You have to run the function twice to round 405 -> 41.

Source: JavaScript math, round to two decimal places

^^ extension usage of Bryce's answer.

like image 20
treyBake Avatar answered Nov 01 '22 14:11

treyBake


You can use toPrecision Function

It will rounded by the precision provided. Here I rounded by 4 to get 6.405 and then rounded by 3 to get 6.41

parseFloat((6.404999999999999).toPrecision(4)).toPrecision(3);

console.log(parseFloat((6.404999999999999).toPrecision(4)).toPrecision(3));
like image 29
Niklesh Raut Avatar answered Nov 01 '22 13:11

Niklesh Raut