dpy15285 2017-07-21 23:00
浏览 33

如何使用简洁的片段确定分层税率? [关闭]

I am trying to create a tiered tax calculator.

The user enters an amount, say 500,000

From that amount, I then have the following tiers that tax is charged as follows:

        0 -   125,000 = 0%
  125,001 -   250,000 = 2%
  250,001 -   925,000 = 5%
  925,001 - 1,500,000 = 10%
1,500,001 -   or more = 12%

So no tax on the first 125,000, then 2% on the next 124,999 and so on.

I can come up with long winded calculations with far too many lines of code. I am sure there is a relatively simple way to do this, so I am bowing to the greater knowledge of folks here to find a more concise method.

  • 写回答

2条回答 默认 最新

  • dongzuo4666 2017-07-22 00:17
    关注

    Hm, nice challenge though. I'm sorry, I couldn't really come up with a smart way, but if you are in dire need I created this little function;

    function calculator($value) {
    
        if(!is_int($value) && !is_double($value))
            return;
    
        if($value > 1500000)
            $value = $value - ($value*0.12);
    
        elseif($value > 925000)
            $value = $value - ($value*0.10);
    
        elseif($value > 250000)
            $value = $value - ($value*0.05);
    
        elseif($value > 125000)
            $value = $value - ($value*0.02);
    
        return $value;
    
    }
    

    You just feed it with a value (int or double) and it will return the calculated value. Be aware you can't use , (comma) though.

    Edit: Changed to logic a little.

    评论

报告相同问题?

悬赏问题

  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析