如何在价格类型更改后删除0

时间:2014-04-28 03:48:19

标签: php

我有一个代码,可以从数据库中更改价格类型。但它没有像我想的那样运行。见例子

1 200 000 000 - > 1亿200密耳(好)

25 500 000 - > 25 mil 50万(ok)

25 000 000 - > 25 mil 0千(不好,我想删除0千)

25 000 - > 0 mil 25千(不太好,我想删除0 mil)

我尝试修复但不起作用。这是我的代码($ number是我从数据库获得的价格)

            $billions = $number % 1000000000;
            $billion = ($number - $billions) / 1000000000; 

            $millions = $billions % 1000000;
            $million = ($billions - $millions) / 1000000; 

            $thousands = $millions % 1000;                
            $thousand = ($millions - $thousands) / 1000;
            if($billion == 0)
            {
                $number=  $million . ' mil ' . $thousand .' thous ' ;
            }
            elseif($million == 0)
            {
                $number=  $thousand .' thous ' ;
            }
            elseif($thousand == 0)
            {
                $number= $billion . ' bil '. $million . ' mil '  ;
            }
            elseif($billion == 0 && $thousand == 0)
            {
                $number=  $million . ' mil ' ;
            }
            else
            {
                $number= $billion . ' bil '. $million . ' mil ' . $thousand .' thous ' ;
            }

3 个答案:

答案 0 :(得分:1)

此功能可能会有所帮助:

<?php

    if (!function_exists('nice_number')) {

    function nice_number($n) {
        // first strip any formatting;
        $n = (0 + str_replace(",", "", $n));
        // is this a number?
        if (!is_numeric($n))
            return false;
        // now filter it;
        $str = '';
        if ($n > 1000000000000) {
            $str = round(($n / 1000000000000), 0) . ' tn ';
            $str .= $n % 1000000000000 > 0 ? nice_number($n % 1000000000000) : '';
            return $str;
        }
        else if ($n > 1000000000) {
            $str = round(($n / 1000000000), 0) . ' bn ';
            $str .= $n % 1000000000 > 0 ? nice_number($n % 1000000000) : '';
            return $str;
        }
        else if ($n > 1000000) {
            $str = round(($n / 1000000), 0) . ' mn ';
            $str .= $n % 1000000 > 0 ? nice_number($n % 1000000) : '';
            return $str;
        }
        else if ($n > 1000)
            return round(($n / 1000), 0) . ' K ';

        return number_format($n);
    }

}

echo nice_number(300200000);

结果:

echo nice_number(300200000); //  outputs 300 mn 200 K

echo nice_number(3000000000); // outputs 300 bn

Here是一个显示答案的小提琴

答案 1 :(得分:0)

而不是拥有所有这些,只需这样做

$number="";

if($billion!=0)
$number=$number .  $billion ' bil ';
if($million!=0)
$number=$number . $million . ' mil ';
if($thousand!=0)
$number=$number . $thousand . 'thousands';

答案 2 :(得分:0)

我决定发布一个答案来补充我上面的评论:

// Sample data
$numbers = array (1200000000,
                  200000,
                  25000,
                  25000000,
                  25500000,
                  320200000,
                 );

foreach ($numbers as $num)
{
    print "{$num} -> " . formatter ($num) . "\n";
}


// Here's the function
function formatter ($number)
{
    $bil = floor ($number / 1000000000);
    $millions = $number % 1000000000;

    $mil = floor ($millions / 1000000);
    $thousands = $millions % 1000000;

    $thou = floor ($thousands / 1000);

    $str = '';

    if ($bil != 0)
    {
        $str .= "{$bil} billions ";
    }
    if ($mil != 0)
    {
        $str .= "{$mil} millions ";
    }
    if ($thou != 0)
    {
        $str .= "{$thou} thousands ";
    }

    return $str;
}

示例输出:

    1200000000 -> 1 billions 200 millions 
    200000 -> 200 thousands 
    25000 -> 25 thousands 
    25000000 -> 25 millions 
    25500000 -> 25 millions 500 thousands 
    320200000 -> 320 millions 200 thousands