douqi3064 2014-05-14 08:41
浏览 45
已采纳

PHP禁用round_up

How can I disable the round up in my PHP script. If I sum up something it only displays the rounded Number.

<?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;
?>

Ok, I guess I've found my mistake. The values are like 3,5 with a , instead of a .

How can I change that if these are values directly from the MySQL DB.?

  • 写回答

3条回答 默认 最新

  • doulun0651 2014-05-14 09:54
    关注

    1.You can use number_format function
    2.optimize your code -there is no need of while loop to do this

     <?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条)

报告相同问题?

悬赏问题

  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换