我必须编写一个函数来将pricetype从(例如:25400000)更改为25 milion 40万。我有一个代码但它只是在价格类型是偶数时改变(例如:25000000)。这是我的代码。
static public function priceFormat($number, $type ='vnd')
{
$subfix = 'vnd';
if ($type == 'vnd'){
$temp = substr($number, -6, -1);
if ($temp == '000000'){
$number = str_replace("000000", '', $number);
$subfix = 'million';
}
}
while (true) {
$replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
if ($replaced != $number) {
$number = $replaced;
} else {
break;
}
}
if ($type != 'vnd')
return $number.' '.$type;
return $number.' '.$subfix;
}
答案 0 :(得分:0)
此代码:
$number = 25400055;
$millionsRemainder = $number % 1000000;
$millions = ($number - $millionsRemainder) / 1000000;
$thousandsRemainder = $millionsRemainder % 1000;
$thousands = ($millionsRemainder - $thousandsRemainder) / 1000;
echo $millions . ' million ' . $thousands . ' thousands ' . ' and ' . $thousandsRemainder;
应该输出“2500万400和55”