【问题标题】:Formatting price in INR method以 INR 方法格式化价格
【发布时间】:2021-02-06 10:28:45
【问题描述】:

我有一段代码,它将数字格式化为 INR 价格(₹1.00/-、₹10,00,000.00/- 等):

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);

Live Demo

另外,代码是开放的,如果有人想让这段代码更清晰、更简短,我很高兴

【问题讨论】:

  • number_format函数。
  • @AlivetoDie 代码共享,有问题 ₹8,42,89,.35(接近十进制)。另外,我要 ₹1.00/- 如果我们只发送 1

标签: php currency number-formatting price


【解决方案1】:

将值设置为$thecash 时,没有使用strposstrpos doc

number_format() 用作:number_format((float)$num,2,'.','');

Working Code:-

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);

【讨论】:

  • 对于大于 3 的十进制值,它会将它们截断为 2 位:3v4l.org/6o0ms。仍然是一个更准确的解决方案。+1
猜你喜欢
  • 1970-01-01
  • 2016-04-26
  • 1970-01-01
  • 1970-01-01
  • 2015-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多