我想总结2个值,比如3,07 + 5.1,我得到结果8,10€,而不是8,17€。我做错了什么?
<?php
$rezultat = "SELECT sum(value) as value FROM values";
$result = mysqli_query($link, $res) or die (mysqli_error($link));
$sum= mysqli_fetch_object($result);
echo number_format($sum->value, 2, ",", "");
mysqli_close($link);
?>
答案 0 :(得分:0)
它似乎是小数点分隔符问题。尝试用“。”替换“,”。像这样:
echo number_format($sum->value, 2, ".", "");
number_format(数,小数,decimalpoint,分离器)
请参阅:http://www.w3schools.com/php/func_string_number_format.asp
答案 1 :(得分:0)
这对我有用:
$kategorija = mysqli_real_escape_string($link, $_POST['kategorija']);
$vrednost = number_format(str_replace("#", ".", preg_replace("/[^0-9#]/i", "", preg_replace("/[\.\,](?!.*[\.\,])/i", "#", $_POST['kolicina']))), 2, ".", "");
$vrednost = mysqli_real_escape_string($link, $vrednost);