Robbie
Robbie

Reputation: 716

show number to 100 decimal places

As an example I have code similar to this:

$r1 = 7.39999999999999;
$r2 = 10000;
echo bcmul($r1,$r2,100);
//returns 74000.0
echo ($r1*$r2);
//returns 74000.0

I am wanting it to return 73999.9999999999 rather than rounding it off. Is there a formula or function to do this?

Upvotes: 0

Views: 80

Answers (2)

clemej
clemej

Reputation: 2563

I'm not a php person, but a quick Google suggests you may want the

$number_format() 

function and specify the $decimals parameter. See this link

Upvotes: 1

Fabian N.
Fabian N.

Reputation: 1240

The doc http://php.net/manual/de/function.bcmul.php says:

left_operand: The left operand, as a string.

right_operand: The right operand, as a string.

So use strings instead:

$r1 = "7.39999999999999";
$r2 = "10000";
echo bcmul($r1,$r2,100);

works.

Or if you have these varibales from somewhere cast them (via (string) ) to string. Maybe at this step you could encounter some roundings already...

Upvotes: 1

Related Questions