Click to See Complete Forum and Search --> : decimal value count in php.


kennyTE
08-21-2007, 02:24 AM
i have a problem, i need to do a program that calculate math value. Seems like i get this wired floating point. note that i need to keep this value in MySQL after calculation, and use in in future to calculate other things.

my code :

<?php
$value_1 = 0.0000000001;

$value_2 = 0.0000080001;

$add_value = $value_1 + $value_2;

echo $add_value;
// the result generate is : 8.0002E-6
?>

Can anyone guide me please, Thanks.

Znupi
08-21-2007, 03:40 AM
There may be an easier way but here's how I'd do it, using number_format() (http://php.net/number-format):


<?php
$value_1 = 0.0000000001;

$value_2 = 0.0000080001;

$add_value = number_format($value_1 + $value_2, 10, '.', '');

echo $add_value;

?>


Should work :)

stephan.gerlach
08-21-2007, 03:48 AM
If I understood you correct then you don't want the result look more like 0.0000080002

if thats right then try this



$value_1 = 0.0000000001;

$value_2 = 0.0000080001;

$add_value = $value_1 + $value_2;

$parts = explode('E-',$add_value);

$parts[1]--;

$result = '0.';

for ($i = $parts[1]; $i>0; $i--) {
$result .= '0';
}

$result .= str_replace('.','',$parts[0]);




I haven't tested it and there is probably a better way to do it. This also only works for E- only.

Hope this helps to get you on the right track .

ADDED LATER:

LOL see there is a loads easier way.

kennyTE
08-21-2007, 07:58 AM
[QUOTE=Znupi]There may be an easier way but here's how I'd do it, using number_format() (http://php.net/number-format):


<?php
$value_1 = 0.0000000001;

$value_2 = 0.0000080001;

$add_value = number_format($value_1 + $value_2, 10, '.', '');

echo $add_value;

?>


Thanks guys, 1st of all, i need to make it compatible with all browser.

with Znupi method, it seems that we have to define the decimal value, look like you define a decimal point for "10", what if i don't know what is the maximum decimal point? what should i do?

As you all see, i need to calculate customer item price, most of the price have long decimal point, if i counted shortage, even shortage of a cent, it will not be accurate. So, with your guys experience, how many decimal point i shoult set to code in, so that i can count more accurate in php?

MrCoder
08-21-2007, 08:39 AM
Just set the value "10" to a number that will never be reached, such as "1000".

Znupi
08-21-2007, 03:33 PM
Better yet, you could do something like:


<?php
$value_1 = 0.0000000001;

$value_2 = 0.0000080001;

$maxDecimals = max(strlen("".$value_1)-2, strlen("".$value_2)-2);

$add_value = number_format($value_1 + $value_2, $maxDecimals, '.', '');

echo $add_value;

?>


I think it should work...

MrCoder
08-21-2007, 03:58 PM
Better yet, you could do something like:


<?php
$value_1 = 0.0000000001;

$value_2 = 0.0000080001;

$maxDecimals = max(strlen("".$value_1)-2, strlen("".$value_2)-2);

$add_value = number_format($value_1 + $value_2, $maxDecimals, '.', '');

echo $add_value;

?>


I think it should work...

What about 10.000000000001?

Znupi
08-21-2007, 04:03 PM
Lol, right, here:

<?php
$value_1 = 0.0000000001;
$value_2 = 0.0000080001;
list( , $len1) = explode(".", "".$value_1);
list( , $len2) = explode(".", "".$value_2);
$maxDecimals = max(strlen($len1), strlen($len2));
$add_value = number_format($value_1 + $value_2, $maxDecimals, '.', '');
echo $add_value;
?>

Didn't test it, but it should work...

Edit: doesn't work, sorry

EDIT 2: Not even the first one doesn't work, lol