使用Java实现向上向下取整
1.背景
业务需求:实现向上取整
2.思考-Math函数
Math.ceil()向上取整函数
Math.floor()向下取整函数
3.解决方案
package top.javai.www; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) public class StuTest01 { /** * 向上取整 */ @Test public void test01(){ Integer a = 120; System.out.println((int) Math.ceil(a/100.0));//double类型数据 } /** * 向下取整 */ @Test public void test02(){ int a = 120; System.out.println((int) Math.floor(a/100.0));//double类型数据 } }
4.完美解决
如果你有更好的解决方案,请给我留言哦.