【问题标题】:Converting to and from CENTS与 CENTS 相互转换
【发布时间】:2011-07-09 20:46:07
【问题描述】:

所以我知道有很多关于金钱和美分转换的问题。 哎呀,我什至问了另一个问题,但我想提出一个稍微不同的问题,所以我希望那里没有重复。

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

$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);
    }
}

我使用的下一个函数是生成 CENTS 或将其作为美元返回。

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个函数将2个函数合并在一起。

function convertMoney($value, $toCents = TRUE) {
    if(isset($value) && strstr($value, ".")) {
        return convertCents(stripMoney($value, 0), stripMoney($value, 1), $toCents);

    } elseif(!empty($value)) {
        return convertCents(stripMoney($value, 0), NULL, $toCents);
    }
}

我所做的可能有点矫枉过正,但我​​认为它相当扎实,除了这 1 个细节,我可以看到。

谁能帮助我进行这些调整?

【问题讨论】:

    标签: php numbers currency money-format


    【解决方案1】:

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

    请查看BC MathGMP extension。后者仅适用于整数,因此您可能对 BC 数学最感兴趣。

    【讨论】:

    • 你说得对,我把它复杂化了……我真正需要做的就是删除除小数点之外的所有内容,将其存储在 MySQL 中的 DECIMAL 字段中,然后使用 BCMath 找出其余部分.
    【解决方案2】:

    我认为 money_format 是您正在寻找的功能...

    <?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 个小数位......
    猜你喜欢
    • 2019-02-20
    • 2019-11-05
    • 2021-06-30
    • 2018-11-18
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    相关资源
    最近更新 更多