php - 将数字分解为数千、数百等

标签 php math currency

我目前正在为我的公司整合支票打印解决方案。打印支票时,您需要打印支付金额中的百万、十万、万、千、百、十和单位(英镑/美元/欧元等)。

在 111232.23 的情况下,以下是我在下面编写的代码的正确输出。我不禁觉得有更有效或更可靠的方法来做到这一点?有谁知道执行此操作的库/类数学技术?

float(111232.23)
Array
(
    [100000] => 1
    [10000] => 1
    [1000] => 1
    [100] => 2
    [10] => 3
    [1] => 2
)

<?php

$amounts = array(111232.23,4334.25,123.24,3.99);

function cheque_format($amount)
{
    var_dump($amount);
    #no need for millions
    $levels = array(100000,10000,1000,100,10,1);
    do{
        $current_level = current($levels);
        $modulo = $amount % $current_level;
        $results[$current_level] = $div = number_format(floor($amount) / $current_level,0);
        if($div)
        {
            $amount -= $current_level * $div;
        }
    }while($modulo && next($levels));

print_r($results);
}

foreach($amounts as $amount)
{
 cheque_format($amount);
}
?>

最佳答案

我想你刚刚重写了 number_format PHP具有的功能。我的建议是使用 PHP 函数而不是重新编写它。

<?php

$number = 1234.56;

// english notation (default)
$english_format_number = number_format($number);
// 1,235

// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56

$number = 1234.5678;

// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57

?>

关于php - 将数字分解为数千、数百等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11669041/

相关文章:

php - Magento 2 - 添加样本数据后的 'Area code not set:'

math - 傅里叶级数在计算机科学方面有什么应用吗?

jSTL - fmt :formatNumber display negative currency in -$xxx. JSTL 中的 xx 格式

php - 如何将印度卢比值(value)传递给 Paypal ?

odoo - Odoo 8/9 是否具有多币种电子商务功能?

PHP\MYSQL\AJAX(?) - 挣扎于如何在不调用页面刷新的情况下动态更新表单

javascript - 如何使用php读取html中标签的id?

php - 检查是否启用了 cookie

c++ - 给定点到给定椭圆的距离

math - Three.js 平面的奇怪现象