Php 用INR方法格式化价格
我有一段代码,将数字格式化为INR价格(₹1.00/- , ₹10,00000.00/-等):Php 用INR方法格式化价格,php,currency,number-formatting,price,Php,Currency,Number Formatting,Price,我有一段代码,将数字格式化为INR价格(₹1.00/- , ₹10,00000.00/-等): 函数价格\u格式($num,$type=1){ $num\u full=数字格式($num,2); if(strpos($num,'.')!==false){ $num=substr($num\u full,0,strpos($num\u full,“.”); } 如果($type==1){/'₹10,00,000.00/-' $explorestunits=“”; 如果(strlen($num)>3
函数价格\u格式($num,$type=1){
$num\u full=数字格式($num,2);
if(strpos($num,'.')!==false){
$num=substr($num\u full,0,strpos($num\u full,“.”);
}
如果($type==1){/'₹10,00,000.00/-'
$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),在将值设置为$thecash
时,不使用strpos
使用number\u格式()
作为:number\u格式((float)$num,2'.','');
:-
函数价格\u格式($num,$type=1){
$num_full=数字_格式((浮点)$num,2,'.','');
if(strpos($num,'.')!==false){
$num=substr($num\u full,0,strpos($num\u full,“.”);
}
如果($type==1){/'₹10,00,000.00/-'
$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;$iLook atnumber\u格式
函数。@验证共享的代码,对于₹ 8,42,89,35(,接近小数点)。另外,我想要₹1.00/-如果我们只发送1个大于3的十进制值,则会将其截断为2位:。这仍然是更精确的解决方案。+1
function price_format($num,$type = 1){
$num_full = number_format($num,2);
if (strpos($num, '.') !== false) {
$num = substr($num_full, 0, strpos($num_full, "."));
}
if($type == 1){ // '₹10,00,000.00/-'
$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.substr($num_full, -3, strpos($num_full, "."))."/-";
} else {
$thecash = "₹".$num.substr($num_full, -3, strpos($num_full, "."))."/-";
}
return $thecash; // writes the final format where $currency is the currency symbol.
}
}
echo price_format(1)."<br />";
echo price_format(10)."<br />";
echo price_format(84289.35);
function price_format($num,$type = 1){
$num_full = number_format((float)$num,2,'.','');
if (strpos($num, '.') !== false) {
$num = substr($num_full, 0, strpos($num_full, "."));
}
if($type == 1){ // '₹10,00,000.00/-'
$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.substr($num_full, -3)."/-";
} else {
$thecash = "₹".$num.substr($num_full, -3)."/-";
}
return $thecash; // writes the final format where $currency is the currency symbol.
}
}
echo price_format(1).PHP_EOL;
echo price_format(10).PHP_EOL;
echo price_format(84289.35);