Jump to content

integer to big, php math calculations


devWhiz

Recommended Posts


<?php

$a = 350000;
$b = 3600000000;

echo $hmm = $a / $b;

 

outputs

 

9.7222222221617E-5

 

while

 


<?php

$a = 350000;
$b = 360000000;

echo $hmm = $a / $b;

 

outputs

 

0.000972222222

 

its only one digit shorter,

 

how can I get the first code to produce the same results, Ive tried using float, int maxes out and Ive changed my floating point in php.ini, no luck

Link to comment
https://forums.phpfreaks.com/topic/237179-integer-to-big-php-math-calculations/
Share on other sites

ok that works but it shortens up the decimal and rounds it, is there a way to have it calculate the decimal without rounding it?

 

like 0.00092144444443

 

rounds to 0.000920

 

how can I get it to keep it at 0.00092144444443

 

bcsub($a,$b);

ok that works but it shortens up the decimal and rounds it, is there a way to have it calculate the decimal without rounding it?

 

like 0.00092144444443

 

rounds to 0.000920

 

how can I get it to keep it at 0.00092144444443

 

 

The correct result of 350000 / 360000000 = 0.0009722222222.. not 0.0009214444.. The reason to why you get an incorrect output is due to the precision loss of a float. Using the BCMath module of PHP you get:

 

echo bcdiv(350000, 360000000, 10); // 0.0009722222

 

10 is the level of precision you need.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.