Php 将价格格式化为逗号分隔的格式
在我的数据库中,我有如下值 256.23200.33,89.33133.45 我必须将这些值乘以千,然后将结果格式化为价格(逗号分隔) 但是如何格式化,我不知道。您正在寻找函数Php 将价格格式化为逗号分隔的格式,php,formatting,Php,Formatting,在我的数据库中,我有如下值 256.23200.33,89.33133.45 我必须将这些值乘以千,然后将结果格式化为价格(逗号分隔) 但是如何格式化,我不知道。您正在寻找函数 $price=123456; echo number_format($price); // output: 123,456 此函数接受一个、两个或四个参数(不是三个): 若只给出一个参数,那个么数字的格式将不带小数,但在每组数千个参数之间使用逗号(“,”) 若给定了两个参数,数字将被格式化为小数,小数前面有一个点(“.
$price=123456;
echo number_format($price);
// output: 123,456
此函数接受一个、两个或四个参数(不是三个):
若只给出一个参数,那个么数字的格式将不带小数,但在每组数千个参数之间使用逗号(“,”)
若给定了两个参数,数字将被格式化为小数,小数前面有一个点(“.”),每组千人之间有一个逗号(“,”)
如果所有四个参数都给定,则数字的格式将为小数点而不是小数点前的点(“.”),并在每组千位之间使用千位而不是逗号(“,”) 检查数字\u格式,下面是一个示例
echo number_format(8333*1000, 3, ',', '.');
$number=1234.56
setlocale(LC_MONETARY,“en_US”)
echo money_格式(“价格为%i”,$number)
//输出为“价格为1234.56美元”
上述答案不考虑小数或四舍五入,这可能有助于需要担心小数的人: 示例:不显示小数使用空格而不是逗号,并使用小数和逗号打印:
$price = 1000000.90;
var_dump(number_format(floor((float) $price), 0, ',', ' '));
var_dump(number_format($price, 2, '.', ','));
输出:
string(9) "1 000 000"
string(12) "1,000,000.90"
下面是使用PHP7将价格转换为印度价格格式的自定义函数+
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.
}
$amount = '10000034000';
$amount = moneyFormatIndia( $amount );
echo $amount;
函数moneyFormatIndia($num){
$explorestunits=“”;
如果(strlen($num)>3){
$lastthree=substr($num,strlen($num)-3,strlen($num));
$restunits=substr($num,0,strlen($num)-3);//提取最后三位数字
$restunits=(strlen($restunits)%2==1)?“0”。$restunits:$restunits;//以2的格式分解剩余的数字,在开头添加一个零以保持2的分组。
$expunit=str_split($restunits,2);
对于($i=0;$i为什么要乘以1000?为什么不保存它呢?嗯,这是一个合理的问题……但实际上这是关于房价和NAR数据。然后将NAR值乘以千,得到实际房价……回音数_格式(8333*1000,2,“,”,“,”);很多公司使用这种格式$4500.00简单但有效。谢谢
$price = 1000000.90;
var_dump(number_format(floor((float) $price), 0, ',', ' '));
var_dump(number_format($price, 2, '.', ','));
string(9) "1 000 000"
string(12) "1,000,000.90"
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.
}
$amount = '10000034000';
$amount = moneyFormatIndia( $amount );
echo $amount;