Round currency closest to five

后端 未结 4 891
庸人自扰
庸人自扰 2021-01-19 05:19

I\'d like to round my values to the closest of 5 cent for example:

5.31 -> 5.30
5.35 -> 5.35
5.33 -> 5.35
5.38 -> 5.40

Currentl

4条回答
  •  广开言路
    2021-01-19 05:56

    I will use round function and NSNumberFormatter also but slightly different algorithm

    I was thinking about using % but I changed it to /

    let formatter = NSNumberFormatter()
    formatter.minimumFractionDigits = 2
    formatter.maximumFractionDigits = 2
    
    //5.30
    formatter.stringFromNumber(round(5.31/0.05)*0.05)
    //5.35
    formatter.stringFromNumber(round(5.35/0.05)*0.05)
    //5.35
    formatter.stringFromNumber(round(5.33/0.05)*0.05)
    //5.40
    formatter.stringFromNumber(round(5.38/0.05)*0.05)
    //12.15
    formatter.stringFromNumber(round(12.13/0.05)*0.05)
    

提交回复
热议问题