【问题标题】:PHP - change number formatPHP - 更改数字格式
【发布时间】:2016-09-07 21:39:36
【问题描述】:

我有这个代码:

<?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,",",".")],但不起作用。谢谢!

【问题讨论】:

  • 这里有几个答案:stackoverflow.com/questions/4325363/…
  • 显示实际查询。除非您明确要求,否则 mysql 不会返回具有“欧元”格式的数字。 Mysql 不对数字 e、g 进行任何“花哨”的格式化。 12345.6 -&gt; 12,345.6
  • 可能是一个字符串

标签: php number-formatting


【解决方案1】:
$num = 1523.45;
echo number_format ( $num, 2, ",", "." );

执行 1.523,45

在你的情况下是

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

希望对你有帮助

【讨论】:

  • 将无法使用 op 的号码是 1523,45 而不是 1523.45
  • 操作谎言。 1523,45 不是(浮点)数字格式。它的字符串格式,因为","。
  • Op 说不,它是一个浮点数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-01
  • 1970-01-01
  • 2021-09-16
  • 2012-08-07
  • 1970-01-01
  • 2012-12-02
相关资源
最近更新 更多