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