Hideki Kan
Hideki Kan

Reputation: 43

Returning a "readable" number in PHP

I am trying to receive a result of a math operation with small numbers (maximum 8 decimals), I receive a float result, but in a format that make the other numbers stay with an error:

$a = round($x, 8); //returns 0.0478674, that's correct
$b = round($y,8); //returns 0.04786261, that's correct
$z = $a - $b; //z returns 4.7899999999976E-6, and not ‭0.00000479‬ as I was expecting

I tried as well

$w = round($z,8); //but w returns 4.79E-6, and not 0.00000479‬ as I was expecting

My problem is because the number 4.7899999999976E-6 give an error in other calcs and it's a ugly number to show to the user.

How can I make this number be 0.00000479‬?

Upvotes: 1

Views: 140

Answers (2)

Sok Hai
Sok Hai

Reputation: 546

you can use number_format:

$w = number_format($z,8);

Upvotes: 1

Peter Barton
Peter Barton

Reputation: 587

number_format should do what you require, from the help: https://www.php.net/manual/en/function.number-format.php

For your specific requirement here:

$w = number_format($z,8);

Upvotes: 0

Related Questions