我是 php 和 mysql 的新手 ..& 我想将钱存储在45,000或21,000 之类的数据库中....
现在我正在使用数据类型decimal(50,2),但是当我输入 45,000 时它只显示 45...
如果我放 45,000 ...那么结果还可以.......但我想像这样放 45,000 ...
我应该使用什么数据类型......所以它可以正确显示?
如果我使用varchar(50),那么我无法对数据进行排序......这就是问题......
请给建议..
谢谢
只需在 PHP 中使用使用decimal(10,2)
字段类型并使用money_format()
用这个
<?php
function money($value) {
return "$".number_format($value);
}
$money = 2000.00;
echo money($money);
// returns $2,000.00
?>
使用以下功能
$amount = '100000';
$amount = moneyFormatIndia( $amount );
echo $amount;
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.
}
// output 1,00,000