简体   繁体   English

将select语句中的mysql编号格式化为php

[英]Formatting a mysql number from a select statement into php

I am using mysql 5.1 database and have a # of fields in them that look like this for format: 我正在使用mysql 5.1数据库,并且其中包含一些格式如下的字段:

1234567 1234567

When I output them to via php, I would like them formatted like this: 1,234,567 当我通过php输出它们时,我希望它们的格式如下:1,234,567

There are no decimals involved. 没有涉及小数。

The select statement is pulling 30 records from my database. select语句从我的数据库中提取30条记录。 Each record has (2) fields that I need formatted thus. 每个记录都有(2)我需要格式化的字段。 Weight and Cost as well as 16 other fields that I need, but do not need formatting. 重量和成本以及我需要的其他16个字段,但不需要格式化。 They are being read and entered int a table. 正在读取它们并将其输入到表中。

Suggestions? 建议?

Note: Data Fields: Cost Weight 注意:数据字段:成本权重
Table: Warships 表:军舰
Current Select statement: 当前选择声明:

$result = mysql_query('SELECT * FROM `Warships` WHERE `Type` = "BC" ');
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['ID'] . "</td>";
  echo "<td>" . $row['SHIP CLASS'] . "</td>";
  echo "<td>" . $row['CAT'] . "</td>";
  echo "<td>" . $row['Type'] . "</td>";
  echo "<td>" . $row['WEIGHT'] . "</td>";
  echo "<td>" . $row['Cost'] . "</td>";

I think PHP's number_format is what you need. 我认为PHP的number_format就是你所需要的。

$num = number_format(1234567);
echo $num; // 1,234,567

If only one parameter is given, number will be formatted without decimals, but with a comma (",") between every group of thousands. 如果只给出一个参数,则数字将被格式化为没有小数,但在每组数千之间使用逗号(“,”)。 More info from PHP API 来自PHP API的更多信息

You're looking for the number_format function. 您正在寻找number_format函数。 Pass your number to it as the first parameter and it will add in a comma for the thousands separator. 将您的号码作为第一个参数传递给它,它将为千位分隔符添加逗号。

Example: 例:

echo "<td>" . number_format($row['Cost']) . "</td>";

number format 数字格式

$num_format = number_format(1234567);
echo $num_format ; // 1,234,567

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM