如何禁用PHP脚本中的舍入。如果我总结一下它只显示圆形数字。
<?php
$con=mysqli_connect("localhost","XXXX","XXXX","XXXX");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM energrid WHERE ID = 1 ");
while($row = mysqli_fetch_array($result)) {
$wert = $row['Wert'];
}
$result = mysqli_query($con,"SELECT * FROM energrid WHERE ID = 3 ");
while($row = mysqli_fetch_array($result)) {
$wert1 = $row['Wert'];
}
mysqli_close($con);
echo "here we go.:";
echo $wert + $wert1;
?>
好吧,我猜我发现了自己的错误。这些值与3,5相似,而不是a。
如果这些是直接来自MySQL DB的值,我怎么能改变呢?
答案 0 :(得分:1)
像这样使用str_replace()
以小数点,
替换逗号.
$a = "3,5";
str_replace(",",".",$a);
答案 1 :(得分:1)
1.您可以使用number_format
功能
2. 优化您的代码 - 没有必要使用while循环来执行此操作
<?php
$db = mysql_connect("localhost","username","password"); ///connect to mysql
if (!$db) {
// throw error if not connect
die("Database connection failed miserably: " . mysql_error());
}
$db_select = mysql_select_db("databasename",$db); ///select database
if (!$db_select) {
//database connection error
die("Database selection also failed miserably: " . mysql_error());
}
$sum=0;
$result = mysqli_query($db,"SELECT sum(wert) as sum FROM energrid WHERE ID in ('1','3') ");
$row = mysqli_fetch_array($result)
$sum =$row['sum'];
`enter code here`
echo number_format($sum,'number','.','');
//where number-no. of place you want to round off
?>
答案 2 :(得分:0)
尝试使用这样的演员:
echo (int)$wert + (int)$wert1;
echo (float)$wert + (float)$wert1;