Best method to round up to the nearest 0.05 in java

前端 未结 3 454
太阳男子
太阳男子 2020-12-06 19:42

Consider that a tax of 10% is applicable on all items except food. Also, an additional tax of of 5 % is applicable on imported items.

If the cost of a music CD is

3条回答
  •  遥遥无期
    2020-12-06 20:15

    You can use long instead of double to use double you can do

    double d = Math.round(d * 20) / 20.0; // round up to multiple of 0.05
    

    to use long (as cents)

    long l = (l + 3) / 5 * 5;
    

    Although its often considered best practice to use int, long or BigDecimal most investment banks and funds use double because once you understand how to use them safely, they are simpler (than long) and faster (than BigDecimal) to use.

提交回复
热议问题