在Scala中打印没有科学记数法的浮点数 [英] print float without scientific notation in scala
本文介绍了在Scala中打印没有科学记数法的浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将浮点数转换为 14 位有效数字
I want to convert float to 14 significant digits
val s = "1200000000".toFloat
输出-1.2E9
我在下面尝试过但不起作用
I tried below but does not work
f"$s%1.0f"
但这不适用于所有值我想要一个接受字符串并返回浮点数的方法.该方法最多可以支持 15 位有效数字
f"$s%1.0f"
but this doesnot work with all values
I want a method which takes in string and return a float .The method can support upto 15 significant digits
推荐答案
您可以使用 toPlainString 方法 java.math.BigDecimal:
You can use toPlainString method of java.math.BigDecimal:
val floatValue = "1200000000".toFloat
new java.math.BigDecimal(floatValue).toPlainString
res0: String = 1200000000
这篇关于在Scala中打印没有科学记数法的浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文