Php 转换成美分或从美分转换成美分

Php 转换成美分或从美分转换成美分,php,numbers,currency,money-format,Php,Numbers,Currency,Money Format,所以我知道有很多关于钱和兑换美分的问题。 见鬼,我甚至问了另一个问题,但我想提出一个稍微不同的问题,所以我希望没有重复的问题 所以我创建了一个函数,它接受一个美元值并将其发送到美分。 但是我认为我的代码有一个小问题,希望我能稍微调整一下 $money4 = "10.0001"; // Converted to cents, as you can see it's slightly off. $money41 = "1001"; // So when "1001", get's put in t

所以我知道有很多关于钱和兑换美分的问题。 见鬼,我甚至问了另一个问题,但我想提出一个稍微不同的问题,所以我希望没有重复的问题

所以我创建了一个函数,它接受一个美元值并将其发送到美分。 但是我认为我的代码有一个小问题,希望我能稍微调整一下

$money4 = "10.0001";
// Converted to cents, as you can see it's slightly off. 
$money41 = "1001";
// So when "1001", get's put in the database, and then I return it back as a Money variable. 
// We get, "$10.01"... but what I have now is a leak in my amounts... as it rounded up to the second point. 
所以为了完成我所做的,我使用了我所做的函数

// This essentially gets a DOLLAR figure, or the CENT's Figure if requested.    
function stripMoney($value, $position = 0, $returnAs = "") 
{   
    // Does it even have a decimal?
    if(isset($value) && strstr($value, ".")) {

        // Strip out everything but numbers, decimals and negative
        $value    = preg_replace("/([^0-9\.\-])/i","",$value);
        $decimals = explode(".", $value);

        // Return Dollars as default
        return ($returnAs == "int" ? (int)$decimals[$position] : $decimals[$position]);

    } elseif(isset($value)) {
        // If no decimals, lets just return a solid number
        $value = preg_replace("/([^0-9\.\-])/i","",$value);
        return ($returnAs == "int" ? (int)$value : $value);
    }
}
我使用的下一个函数是生成美分或将其作为美元返回

function convertCents($money, $cents = NULL, $toCents = TRUE)
{
    if(isset($money)) {
        if($toCents == TRUE) {
            // Convert dollars to cents
            $totalCents = $money * 100;
            // If we have any cents, lets add them on as well
            if(isset($cents)) {
                $centsCount = strlen($cents);
                // In case someone inputs, $1.1
                // We add a zero to the end of the var to make it accurate
                if($centsCount < 2) {
                    $cents = "{$cents}0";   
                }
                // Add the cents together
                $totalCents = $totalCents + $cents;
            }
            // Return total cents
            return $totalCents;

        } else {
            // Convert cents to dollars
            $totalDollars = $money / 100;
            return $totalDollars;
        }
    }
}
我所做的可能有些过火,但我认为除了这1个细节之外,它是相当可靠的,我可以看到


有人能帮我做这些调整吗?

如果需要精确的答案,请不要使用浮点运算。这适用于几乎所有语言,而不仅仅是PHP。请阅读下面的大警告

取而代之的是退房或退房。后者只适用于整数,因此您可能对BC数学最感兴趣。

我认为是您正在寻找的函数

<?php

$number = 1234.56;

// let's print the international format for the en_US locale
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number) . "\n";
// USD 1,234.56

// Italian national format with 2 decimals`
setlocale(LC_MONETARY, 'it_IT');
echo money_format('%.2n', $number) . "\n";
// Eu 1.234,56

?>

是的,我可以格式化我的数字,好吧,我只是很难得到我的4个十进制位置……你说的对,我把它复杂化了……我真正需要做的就是删除所有内容,但小数点,用十进制字段将其存储在MySQL中,然后使用BCMath计算出其余部分。
<?php

$number = 1234.56;

// let's print the international format for the en_US locale
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number) . "\n";
// USD 1,234.56

// Italian national format with 2 decimals`
setlocale(LC_MONETARY, 'it_IT');
echo money_format('%.2n', $number) . "\n";
// Eu 1.234,56

?>