kotlin 小数点で四捨五入を実行する

kotlinで、小数点で四捨五入を実行する手順を記述してます。「kotlin.math.roundToInt」の「roundToInt」で可能です。
環境
- OS windows11 home
- java 17.0.2
- kotlin 1.6.10-release-923
手順
小数点で四捨五入を実行するには、「roundToInt」を使用します。
import kotlin.math.roundToInt
値.roundToInt()
実際に、四捨五入してみます。
import kotlin.math.roundToInt
fun main() {
var x: Double = 123.45
var y: Double = 123.99
check(x.roundToInt()) // 123 is Int
check(y.roundToInt()) // 124 is Int
}
fun check(x: Any?){
when (x) {
is Byte -> println("$x is Byte")
is Int -> println("$x is Int")
is Double -> println("$x is Double")
is Char -> println("$x is Char")
is String -> println("$x is String")
is Boolean -> println("$x is Boolean")
}
}
四捨五入されていることが確認できます。
-
前の記事
javascript エラー「Uncaught TypeError: xxx.toUTCString is not a function」の解決方法 2023.03.29
-
次の記事
Finder アプリケーションを開くショートカットキー 2023.03.29
コメントを書く