Using ints w/decimal place tracking for handling currency in PHP
I have an issue, heh. I'm doing some calcs with bitcoins in PHP. When I call to the API, I get back JSON with bitcoin values. They are strings with a precision of 1millionth of a decimal.
I was tracking them as floats in my app, but that's leading to many precision issues and loss.
So, I decided to translate each string to an integer and store the decimal "power". I perform an operation, say subtraction, and then return the result to their original power.
My code (I know it looks like crap, been messing with this all night):
$val1 = 12.01;
$val2 = 11.01;
translateBTC($val1, $val1T, $val1Pow);
translateBTC($val2, $val2T, $val2Pow);
echo "Subtracing $val1 - $val2\n";
echo "Val1 = $val1T, power = $val1Pow\n";
echo "Val2 = $val2T, power = $val2Pow\n";
subtractBTC($val1T, $val1Pow, $val2T, $val2Pow, $res, $resP);
echo "Result = $res\n";
function translateBTC($btc, &$val, &$pow)
{
$v1Str = (string) $btc;
$v1DPos = strpos($v1Str, '.');
$v1Int = '';
$v1Last = 0;
for($i=0;$i<strlen($v1Str);$i++)
{
if ($v1Str[$i] != '.')
{
$v1Int .= $v1Str[$i];
}
if ($seen)
{
if ($v1Str[$i] != 0)
$v1Last = $i;
}
if ($v1Str[$i] == '.')
{
// Were now tracking past the ., so pow.
$seen = TRUE;
}
}
$val = (int) $v1Int;
$pow = $v1Last - $v1DPos;
}
function subtractBTC($val1, $val1P, $val2, $val2P, &$result, &$rPow)
{
$highP = ($val1P > $val2P)? $val1P : $val2P;
$lowP = ($val1P < $val2P)? $val1P : $val2P;
$pStr = str_pad('', ($highP - $lowP), '0');
$val2 .= ($val1 > $val2)? $pStr : '';
$val1 .= ($val2 > $val1)? $pStr : '';
$val1 = (int)$val1;
$val2 = (int)$val2;
echo "val1($val1) - val2($val2)\n";
$tmpResult = $val1 - $val2;
$rPow = $highP;
$tmpResult = (string) $tmpResult;
echo "tmpResult = ($tmpResult), rPow = $rPow\n";
...
At this point, I'm struggling with converting the result back to the proper base, for all the different values...I've got to be going about this all wrong - is there a curre开发者_StackOverflow社区ncy type I can use for this sort of precision?
Here's the debug output:
Val1 = 1201, power = 2
Val2 = 1101, power = 2
val1(1201) - val2(1101)
tmpResult = (100), rPow = 2
001Result = 10.0
TIA SO!
Look into the BC Math extension which lets you operate on arbitrary precision numbers stored as strings.
精彩评论