dqx36753 2017-03-03 06:01
浏览 51

如何将整数值转换为lacs或crores(印度编号系统)

I want to convert the integer value to Indian Numbering system. I am giving the value 540000 as input and getting the output value 5.4 Lac, but I want answer 5.40 Lac.

My Code:

<?php
function no_to_words($no)
{
    if($no == 0) {
        return ' ';

    }else {
        $n =  strlen($no); // 7
        switch ($n) {
            case 3:
                $val = $no/100;
                $val = round($val, 2);
                $finalval =  $val ." Hundred";
                break;
            case 4:
                $val = $no/1000;
                $val = round($val, 2);
                $finalval =  $val ." Thousand";
                break;
            case 5:
                $val = $no/1000;
                $val = round($val, 2);
                $finalval =  $val ." Thousand";
                break;
            case 6:
                $val = $no/100000;
                $val = round($val, 2);
                $finalval =  $val ." Lac";
                break;
            case 7:
                $val = $no/100000;
                $val = round($val, 2);
                $finalval =  $val ." Lac";
                break;
            case 8:
                $val = $no/10000000;
                $val = round($val, 2);
                $finalval =  $val ." Cr";
                break;
            case 9:
                $val = $no/10000000;
                $val = round($val, 2);
                $finalval =  $val ." Cr";
                break;

            default:
                echo "";
        }
        return $finalval;

    }
}

?>
  • 写回答

1条回答 默认 最新

  • dongshiliao7990 2017-03-04 06:06
    关注

    Your problem isn't really this function, since numbers 5.4 and 5.40 are the same number, so either you need to apply formatting here, or in the place where you're using the results. Check the difference of these 2:

    echo round(540000/100000, 2);
    

    vs

    echo sprintf("%2.2f", round(540000/100000, 2));
    

    The second one will show 2 decimals, since that's defined in the format. You don't really need the round either if you use sprintf, it will round the number to the specified format.

    echo sprintf("%2.2f", 555555 / 100000);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭