0

我有一个循环,它从我的数据库中添加结果。这些值是这种格式的数字字符串:$$$$$$.$$ 我无法将结果相加并以相同的格式显示:

function totals (){ 
    $result = mysql_query("SELECT meta_value FROM postmeta WHERE postmeta.meta_key = 'Value'");
    $value = 0;
    while( $row = mysql_fetch_array($result) ) {
        $value = $value + str_replace(',', '', $row['meta_value']);
    }
    echo "Worth $".$value."!";
};

使用上述内容,我得到了价值 653987.32 美元!现在我需要重新插入逗号,所以我试试这个:

$value = number_format($value, 2, '.', ',');

但后来我得到了价值 10,000.00 美元!

按要求输出:

    original: 5,000.00   numeric: 5 
    original: 0.00   numeric: 0 
    original: 100.00   numeric: 100 
    original: 19.95   numeric: 20 
    original: 28.00   numeric: 28 
    original: 3,777.00   numeric: 3 
    original: 2,500.00   numeric: 2 
    original: 575.00   numeric: 575 
    original: 4,000.00   numeric: 4 
    original: 100.00   numeric: 100 
    original: 1,500.00   numeric: 1 
    original: 0.00   numeric: 0 
    original: 343.50   numeric: 344 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 1,070.00   numeric: 1 
    original: 305.00   numeric: 305 
    original: 1,000.00   numeric: 1 
    original: 0.00   numeric: 0 
    original: 50.00   numeric: 50 
    original: 1144.99   numeric: 1,145 
    original: 900.00   numeric: 900 
    original: 3,750.00   numeric: 3 
    original: 1,750   numeric: 1 
    original: 50.00   numeric: 50 
    original: 50.00   numeric: 50 
    original: 50.00   numeric: 50 
    original: 200.00   numeric: 200 
    original: 619.00   numeric: 619 
    original: 200.00   numeric: 200 
    original: 50.00   numeric: 50 
    original: 0.00   numeric: 0 
    original: 2,699.00   numeric: 2 
    original: 0.00   numeric: 0 
    original: 10,000.00   numeric: 10 
    original: 500.00   numeric: 500 
    original: 186.00   numeric: 186 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 39,000.00   numeric: 39 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 8,330.00   numeric: 8 
    original: 0.00   numeric: 0 
    original: 50.00   numeric: 50 
    original: 50.00   numeric: 50 
    original: 100.00   numeric: 100 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 4,875.00   numeric: 4 
    original: 1,000.00   numeric: 1 
    original: 3,135.00   numeric: 3 
    original: 15,000.00   numeric: 15 
    original: 400.00   numeric: 400 
    original: 75.00   numeric: 75 
    original: 100.00   numeric: 100 
    original: 6,893.00   numeric: 6 
    original: 330.00   numeric: 330 
    original: 484.96   numeric: 485 
    original: 8,600.00   numeric: 8 
    original: 2,500.00   numeric: 2 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 6,150.00   numeric: 6 
    original: 500.00   numeric: 500 
    original: 275.00   numeric: 275 
    original: 900.00   numeric: 900 
    original: 750.00   numeric: 750 
    original: 450.00   numeric: 450 
    original: 4,600.00   numeric: 4 
    original: 299.50   numeric: 300 
    original: 250.00   numeric: 250 
    original: 825.00   numeric: 825 
    original: 750.00   numeric: 750 
    original: 50.00   numeric: 50 
    original: 2,000.00   numeric: 2 
    original: 16,000   numeric: 16 
    original: 2,600.00   numeric: 2 
    original: 240.00   numeric: 240 
    original: 4,500.00   numeric: 4 
    original: 0.00   numeric: 0 
    original: 6,000.00   numeric: 6 
    original: 0.00   numeric: 0 
    original: 3,000.00   numeric: 3 
    original: 0.00   numeric: 0 
    original: 15,000.00   numeric: 15 
    original: 45,987.24   numeric: 45 
    original: 1,100.00   numeric: 1 
    original: 20,000.00   numeric: 20 
    original: 5,100.00   numeric: 5 
    original: 12,000.00   numeric: 12 
    original: 0.00   numeric: 0 
    original: 550.00   numeric: 550 
    original: 50.00   numeric: 50 
    original: 50.00   numeric: 50 
    original: 150.00   numeric: 150 
    original: 4,500.00   numeric: 4 
    original: 1,500.00   numeric: 1 
    original: 0.00   numeric: 0 
    original: 1,758.00   numeric: 1 
    original: 450.00   numeric: 450 
    original: 400.00   numeric: 400 
    original: 28.00   numeric: 28 
    original: 20.00   numeric: 20 
    original: 50.00   numeric: 50 
    original: 20.00   numeric: 20 
    original: 22.00   numeric: 22 
    original: 99.00   numeric: 99 
    original: 500.00   numeric: 500 
    original: 500.00   numeric: 500 
    original: 33,080.00   numeric: 33 
    original: 3,000.00   numeric: 3 
    original: 5,000.00   numeric: 5 
    original: 25,000.00   numeric: 25 
    original: 25,750.00   numeric: 25 
    original: 5,000.00   numeric: 5 
    original: 1,800.00   numeric: 1 
    original: 419.75   numeric: 420 
    original: 600.00   numeric: 600 
    original: 130.00   numeric: 130 
    original: 1,000.00   numeric: 1 
    original: 650.00   numeric: 650 
    original: 650.00   numeric: 650 
    original: 285.00   numeric: 285 
    original: 949.00   numeric: 949 
    original: 500.00   numeric: 500 
    original: 798.80   numeric: 799 
    original: 279.50   numeric: 280 
    original: 369.90   numeric: 370 
    original: 0.00   numeric: 0 
    original: 5,000.00   numeric: 5 
    original: 1,995.00   numeric: 1 
    original: 1,800.00   numeric: 1 
    original: 0.00   numeric: 0 
    original: 50.00   numeric: 50 
    original: 50.00   numeric: 50 
    original: 50.00   numeric: 50 
    original: 3,316.00   numeric: 3 
    original: 50.00   numeric: 50 
    original: 50.00   numeric: 50 
    original: 0.00   numeric: 0 
    original: 50.00   numeric: 50 
    original: 17,182.28   numeric: 17 
    original: 30,000.00   numeric: 30 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 880.00   numeric: 880 
    original: 12.00   numeric: 12 
    original: 100.00   numeric: 100 
    original: 100.00   numeric: 100 
    original: 100.00   numeric: 100 
    original: 5,600.00   numeric: 5 
    original: 1,600.00   numeric: 1 
    original: 100.00   numeric: 100 
    original: 14.95   numeric: 15 
    original: 6,000.00   numeric: 6 
    original: 7,400.00   numeric: 7 
    original: 750.00   numeric: 750 
    original: 1,575.00   numeric: 1 
    original: 50.00   numeric: 50 
    original: 3,500.00   numeric: 3 
    original: 5,000.00   numeric: 5 
    original: 1,000.00   numeric: 1 
    original: 6,500.00   numeric: 6 
    original: 14,375.00   numeric: 14 
    original: 9,500.00   numeric: 9 
    original: 91,343.00   numeric: 91 
    original: 22,610.00   numeric: 22 
    original: 15,000.00   numeric: 15 
    original: 10,000.00   numeric: 10 

显然有些不对劲。任何人都可以帮忙吗?

numeric total: 661,379 Original total: 661379.32
4

3 回答 3

2

它输出什么?听起来您的数字可能太大而无法容纳 31 位,但在我看到您的确切输出之前很难知道。

你确定你所有的$row['meta_value']值都是带逗号的直数吗?尝试这样做:

while($row = mysql_fetch_array($result)) {
    echo "Value: {$row['meta_value']}\n";
    $value += str_replace(',', '', $row['meta_value']);
}

echo "final value: $value\n";
echo "formatted: ".number_format($value, 2)."\n";

我想知道是否某些值不符合您的预期。

于 2012-04-26T00:49:10.560 回答
0

您也可以将值转换为浮点数:

(float)str_replace(',','',$row['meta_value'])

编辑:

完整代码:

function totals () { 
    $result = mysql_query("SELECT meta_value FROM postmeta WHERE postmeta.meta_key = 'Value'");
    $value = 0;
    while ($row = mysql_fetch_array($result)) {
        $value += (float)str_replace(',', '', $row['meta_value']);
    }
    echo "Worth \$$value!";
};
于 2012-04-26T01:19:50.530 回答
0

您还可以从查询本身正确格式化它们并在您想要的字段上操作:

SELECT replace(meta_value, ',', '') as number_value, meta_value as original_value
FROM postmeta
WHERE postmeta.meta_key = 'Value'
于 2012-04-26T01:24:05.570 回答