将价格类型转换为xxx milion xxx千

时间:2014-04-27 02:55:21

标签: php yii

我必须编写一个函数来将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;
}

1 个答案:

答案 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”