在数字中添加逗号
Posted
技术标签:
【中文标题】在数字中添加逗号【英文标题】:add commas in number 【发布时间】:2015-10-23 06:06:35 【问题描述】:<html> <head> <title>Rough Diamond
Information</title> </head>
<body>
<?php
mysql_connect("localhost","root","");
mysql_select_db("basic");
$order = "select * from calculator ";
$result=mysql_query($order);
echo "Rough Diamonds Information: ";
echo "<a href='home.html'>Go to main page <br><br>";
?>
<table border="1" style="width:50%">
<tr>
<th><b>ID</b></th>
<th><b> Name</b></th>
<th><b> Total Rough Weight</b></th>
<th><b>One Carat Price</b></th>
<th><b>Dollar Rate</b></th>
<th><b>Payment Days</b></th>
<th><b>Total Payment</b></th>
</tr>
<?php while($row = mysql_fetch_array($result) )
echo "<tr>
<td>".$row['id']."</td>
<td>".$row['name']."</td>
<td>".$row['total_wt']."</td>
<td>".$row['crt_price']."</td>
<td>".$row['dollar_rate']."</td>
<td>".$row['pay_day']."</td>
<td>".$row['total_price']."</td>
</tr>"; ?>
</body>
</html>
【问题讨论】:
请有人以可见格式编辑 dis 代码 请指定错误和所需的输出 此代码用于从数据库中获取值。我想要这种格式的所有值。例如(3,00,000) 【参考方案1】:使用number_format($number);
。
查看http://php.net/manual/en/function.number-format.php
【讨论】:
<td>".$row['total_price']."</td>
--> <td>".number_format($row['total_price'])."</td>
【参考方案2】:
使用money_format
像这样格式化货币。
$amount = '300000';
setlocale(LC_MONETARY, 'en_IN');
$amount = money_format('%!i', $amount);
echo $amount; // 3,00,000.00
这里是帮助链接Link
【讨论】:
【参考方案3】:number_format 适合你
number_format($row['number'],0);
【讨论】:
【参考方案4】:创建一个函数并显示数字如下
function moneyFormatIndia($num)
$explrestunits = "" ;
if(strlen($num)>3)
$lastthree = substr($num, strlen($num)-3, strlen($num));
$restunits = substr($num, 0, strlen($num)-3); // extracts the last three digits
$restunits = (strlen($restunits)%2 == 1)?"0".$restunits:$restunits; // explodes the remaining digits in 2's formats, adds a zero in the beginning to maintain the 2's grouping.
$expunit = str_split($restunits, 2);
for($i=0; $i<sizeof($expunit); $i++)
// creates each of the 2's group and adds a comma to the end
if($i==0)
$explrestunits .= (int)$expunit[$i].","; // if is first value , convert into integer
else
$explrestunits .= $expunit[$i].",";
$thecash = $explrestunits.$lastthree;
else
$thecash = $num;
return $thecash; // writes the final format where $currency is the currency symbol.
<?php while($row = mysql_fetch_array($result) )
echo "<tr>
<td>".$row['id']."</td>
<td>".$row['name']."</td>
<td>".$row['total_wt']."</td>
<td>".$row['crt_price']."</td>
<td>".$row['dollar_rate']."</td>
<td>".$row['pay_day']."</td>
<td>".moneyFormatIndia($row['total_price'])."</td>
</tr>"; ?>
【讨论】:
以上是关于在数字中添加逗号的主要内容,如果未能解决你的问题,请参考以下文章