Python Java Android Django Web -> [email protected]

Float / Double

Math.ceil()

1
2
3
4
5
6
7
8
9
10
11
val a = 100.0675
val d = 100.675
val e = 100.500
val f = 100.0
val g = 90.0

println("Math.ceil($a) -> ${Math.ceil(a)}")
println("Math.ceil($d) -> ${Math.ceil(d)}")
println("Math.ceil($e) -> ${Math.ceil(e)}")
println("Math.ceil($f) -> ${Math.ceil(f)}")
println("Math.ceil($g) -> ${Math.ceil(g)}")

Output:

1
2
3
4
5
Math.ceil(100.0675) -> 101.0
Math.ceil(100.675) -> 101.0
Math.ceil(100.5) -> 101.0
Math.ceil(100.0) -> 100.0
Math.ceil(90.0) -> 90.0

Math.round()

实现Flaot/Double同四舍五入效果。

1
2
3
4
5
6
7
8
9
10
11
val a = 100.0675
val d = 100.675
val e = 100.500
val f = 100f
val g = 90f

println("Math.round($a) -> ${Math.round(a)}")
println("Math.round($d) -> ${Math.round(d)}")
println("Math.round($e) -> ${Math.round(e)}")
println("Math.round($f) -> ${Math.round(f)}")
println("Math.round($g) -> ${Math.round(g)}")

Output:

1
2
3
4
5
Math.round(100.0675) -> 100
Math.round(100.675) -> 101
Math.round(100.5) -> 101
Math.round(100.0) -> 100
Math.round(90.0) -> 90

Long

Avoid Overflow

1
2
3
4
5
6
try {
val result = Math.multiplyExact(Long.MAX_VALUE, Long.MAX_VALUE)
} catch (e: ArithmeticException) {
return
}
Assert.fail()