PHP - 更改数字格式

时间:2016-09-07 21:39:37

标签: php number-formatting

我有这段代码:

<?php
if(isset($_POST['get_luna_inceput'])){
  $ret =[];
  $sch_luna_inceput = $_POST['get_luna_inceput'];
  $sql = "SELECT
            ...";
  $result = $conn->query($sql);
  if ($result->num_rows > 0){
  while($row = $result->fetch_assoc()){
    $ret[] =[$row['JUDET'], floatval($row['TOTAL'])];
  }
  }
}
  echo json_encode($ret) ;
?>

我的代码sql以数字格式(1523,45)返回$row['TOTAL']的值。如何更改格式(1.523,45)?我试过这个:$ret[] =[$row['JUDET'], number_format(floatval($row['TOTAL']),2,",",".")]而不是工作。谢谢!

1 个答案:

答案 0 :(得分:2)

$num = 1523.45;
echo number_format ( $num, 2, ",", "." );

执行1.523,45

在你的情况下它将是

$ret[] = [ $row['JUDET'], number_format( $row['TOTAL']), 2, ",", ".") ];

希望有所帮助