Kotlin整数相除精度损失roundToInt

发布于:2025-04-21 ⋅ 阅读:(23) ⋅ 点赞:(0)

Kotlin整数相除精度损失roundToInt

import kotlin.math.roundToInt

fun main() {
    val a = 0.0f

    val delta = 0.1f

    for (i in 0..10) {
        val r = a + i * delta

        println("float=${r} toInt=${r.toInt()} (+0.5 toInt)=${(r + 0.5).toInt()} round=${Math.round(r)} roundToInt=${r.roundToInt()}")
    }
}

float=0.0 toInt=0 (+0.5 toInt)=0 round=0 roundToInt=0
float=0.1 toInt=0 (+0.5 toInt)=0 round=0 roundToInt=0
float=0.2 toInt=0 (+0.5 toInt)=0 round=0 roundToInt=0
float=0.3 toInt=0 (+0.5 toInt)=0 round=0 roundToInt=0
float=0.4 toInt=0 (+0.5 toInt)=0 round=0 roundToInt=0
float=0.5 toInt=0 (+0.5 toInt)=1 round=1 roundToInt=1
float=0.6 toInt=0 (+0.5 toInt)=1 round=1 roundToInt=1
float=0.7 toInt=0 (+0.5 toInt)=1 round=1 roundToInt=1
float=0.8 toInt=0 (+0.5 toInt)=1 round=1 roundToInt=1
float=0.90000004 toInt=0 (+0.5 toInt)=1 round=1 roundToInt=1
float=1.0 toInt=1 (+0.5 toInt)=1 round=1 roundToInt=1

Kotlin toInt() +0.5 toInt() Math.round()_kotlin math.round-CSDN博客文章浏览阅读496次,点赞4次,收藏4次。Java小数点数字和百分号数字之间的转换小数点数字和百分号(百分比)数字之间的转换在证券金融业很常见,需要经常进行两者之间相互转换。如代码: String s1 = "21.8%";Java小数点数字和百分号数字之间的转换_将浮点型转为百分比 java-CSDN博客。Kotlin浮点数Float整数Int乘*除/精度损失。Kotlin浮点数Float整数Int乘*除/精度损失-CSDN博客。_kotlin math.round https://blog.csdn.net/zhangphil/article/details/135850802

Kotlin浮点数Float整数Int乘*除/精度损失_kotlin float乘法 精度丢失-CSDN博客文章浏览阅读1k次,点赞7次,收藏10次。文章讨论了Kotlin中浮点数(如1.7f)与整数(如Int)进行乘法、除法时可能出现的精度损失,并给出了示例。同时,提到了Java中如何将小数点数字转换为百分比格式的过程,使用了`NumberFormat`类。 https://blog.csdn.net/zhangphil/article/details/135630516


网站公告

今日签到

点亮在社区的每一天
去签到