Php and javascript money format does not give the same result

Hello,

What should I do to get the same result for both?

Source: 123,62957

in php

number_format('123,62957',3,",",".")
Out: 123,630

in javascript

toFixed(3)
Out: 123,629
$number = 123.62957;
$decimals = 3;

$factor = pow(10, $decimals);
$number = floor($number * $factor) / $factor;

$formatted_number = number_format($number, $decimals, ",", ".");

echo $formatted_number;  // Output: 123,629

and in JavaScript

function roundUp(number, decimals) {
  const factor = Math.pow(10, decimals);
  return (Math.ceil(number * factor) / factor).toFixed(decimals);
}

const myNumber = 123.62957;
const roundedUp = roundUp(myNumber, 3);

console.log(roundedUp);  // Output: 123.630

1 Like

Got it, thank you very much

Sponsor our Newsletter | Privacy Policy | Terms of Service