用空格分开数字

时间:2013-02-11 21:47:51

标签: php numbers space

我想用这样的空格分隔数字:1 200,12 500或2 000 000 而是2000000 ......

function show_price($id) {
    global $currency_a;
    if(get_option("defaultcurrency")) {
        $curr = $currency_a[get_option("defaultcurrency")][2];
    } else {
        $curr = $currency_a[get_post_meta($id, "currency", true)][2];
    }
    $price = get_post_meta($id, "price", true);
    $currpos = get_option("currpos");
    if($currpos == "1" || !$currpos) {
        return $price."".$curr;
    } elseif ($currpos == "2") {
        return $curr."".$price;
    } elseif ($currpos == "3") {
        return $price;
    }
}

1 个答案:

答案 0 :(得分:5)

PHP有自己的功能:number_format()

http://php.net/manual/en/function.number-format.php

只需将$ thousands_sep参数设置为空格而不是默认值。例如:

number_format($number, 0, '.', ' ');