Noe*_*lia 22 java android rounding kotlin
我有一个Double变量,0.0449999
我想将它舍入到1位小数0.1
.
我正在使用Kotlin,但Java解决方案也很有帮助.
val number:Double = 0.0449999
我尝试使用这两个解决方案获得1位小数:
val solution = Math.round(number * 10.0) / 10.0
val solution = String.format("%.1f", number)
问题是我在两种情况下都得到0.0,因为它将数字舍入0.04
到0.0
.它不会取所有小数并将其舍入.
我想获得0.1: 0.045 -> 0.05 -> 0.1
Noe*_*lia 37
最后我做了Andy Turner
建议,舍入到3位小数,然后到2然后到1:
答案1:
val number:Double = 0.0449999
val number3digits:Double = String.format("%.3f", number).toDouble()
val number2digits:Double = String.format("%.2f", number3digits).toDouble()
val solution:Double = String.format("%.1f", number2digits).toDouble()
Run Code Online (Sandbox Code Playgroud)
答案2:
val number:Double = 0.0449999
val number3digits:Double = Math.round(number * 1000.0) / 1000.0
val number2digits:Double = Math.round(number3digits * 100.0) / 100.0
val solution:Double = Math.round(number2digits * 10.0) / 10.0
Run Code Online (Sandbox Code Playgroud)
结果:
0.045→0.05→0.1
注意:我知道它不应该如何工作但有时你需要考虑到一些特殊情况下考虑所有小数,所以也许有人觉得这很有用.
hot*_*key 26
所述BigDecimal
舍入具有几个RoundingMode
S,包括那些围捕(远离零)或朝向正无穷大.如果这就是您所需要的,您可以通过调用setScale
如下进行舍入:
val number = 0.0449999
val rounded = number.toBigDecimal().setScale(1, RoundingMode.UP).toDouble()
println(rounded) // 0.1
Run Code Online (Sandbox Code Playgroud)
但请注意,它的工作方式是将之间也有圆形东西0.0
,并0.1
以0.1
(例如0.00001
→ 0.1
).
该.toBigDecimal()
扩展程序自Kotlin 1.2起可用.
小智 16
Float 和 Double 的扩展函数示例,四舍五入到 n 个小数位。
fun Float.roundTo(n : Int) : Float {
return "%.${n}f".format(this).toFloat()
}
fun Double.roundTo(n : Int) : Double {
return "%.${n}f".format(this).toDouble()
}
Run Code Online (Sandbox Code Playgroud)
jaf*_*aml 15
在 Kotlin 中我只使用这个函数:
fun roundTheNumber(numInDouble: Double): String {
return "%.2f".format(numInDouble)
}
Run Code Online (Sandbox Code Playgroud)
Gau*_*oda 12
我知道上面的一些解决方案可以完美地工作,但是我想添加另一个使用ceil和floor概念的解决方案,我认为该解决方案已针对所有情况进行了优化。
如果要十进制后两位的最大值,请使用此值。
-import java.math.BigDecimal
-import java.math.RoundingMode
-import java.text.DecimalFormat
Run Code Online (Sandbox Code Playgroud)
在这里1.45678 = 1.46
fun roundOffDecimal(number: Double): Double? {
val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.CEILING
return df.format(number).toDouble()
}
Run Code Online (Sandbox Code Playgroud)
如果要在小数点后两位的最小值,则使用此值。
在这里1.45678 = 1.45
fun roundOffDecimal(number: Double): Double? {
val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.FLOOR
return df.format(number).toDouble()
}
Run Code Online (Sandbox Code Playgroud)
还有其他类似下面的标志 1.楼层2.天花板3.向下4.半场5.半场6.半场7.不必要的8.场
详细信息在docs中给出
kot*_*oMJ 12
始终提防语言环境!
使用未指定的语言环境,您可能会偶尔遇到问题(例如葡萄牙语言环境),例如
Fatal Exception: java.lang.NumberFormatException
For input string: "0,1"
Run Code Online (Sandbox Code Playgroud)
1.使用DecimalFormat方法的解决方案
fun Float.roundToOneDecimalPlace(): Float {
val df = DecimalFormat("#.#", DecimalFormatSymbols(Locale.ENGLISH)).apply {
roundingMode = RoundingMode.HALF_UP
}
return df.format(this).toFloat()
}
Run Code Online (Sandbox Code Playgroud)
2. 使用字符串格式方法的解决方案
fun Float.roundTo(decimalPlaces: Int): Float {
return "%.${decimalPlaces}f".format(Locale.ENGLISH,this).toFloat()
}
Run Code Online (Sandbox Code Playgroud)
1.方法(使用Noelia的想法):
您可以在字符串模板中传递所需的小数位数,然后通过以下方式使precision变量:
fun Number.roundTo(numFractionDigits: Int)
= String.format("%.${numFractionDigits}f", toDouble()).toDouble()
Run Code Online (Sandbox Code Playgroud)
2.方法(数字,无字符串转换)
fun roundToDecimals(number: Double, numDecimalPlaces: Int): Double {
val factor = Math.pow(10.0, numDecimalPlaces.toDouble())
return Math.round(number * factor) / factor
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
26802 次 |
最近记录: |