gpt4 book ai didi

php - 添加带逗号的数字字符串

转载 作者:行者123 更新时间:2023-11-29 08:54:33 25 4
gpt4 key购买 nike

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

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."!";
};

使用上面的内容,我得到类似“Worth $653987.32!”的结果。现在我需要重新插入逗号,所以我尝试这样做:

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

但是我得到了 10,000.00 美元的值(value)!

按要求输出:

    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

最佳答案

它输出什么?听起来你的数字可能太大,无法容纳 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";

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

关于php - 添加带逗号的数字字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10325798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com