Scala 两个整数相除得到浮点结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11123572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala Divide two integers and get a float result
提问by richsoni
If I do the following
如果我执行以下操作
println(3/4)
>0
I would like to get a decimal answer instead of an integer. Because of the way I am printing in my actual code I would prefer to cast within the println if that is possible.
我想得到一个十进制的答案而不是一个整数。由于我在实际代码中打印的方式,如果可能的话,我更愿意在 println 中进行转换。
回答by Rex Kerr
If you're typing the number, just type at least one of them as a float (I assume you mean Floatnot Double):
如果您正在输入数字,只需将其中至少一个输入为浮点数(我假设您的意思是Float不是Double):
println(3f/4)
If you already have the numbers in variables, convert at least one of them with toFloat
如果您已经有变量中的数字,请至少转换其中一个 toFloat
val x = 3
println(x.toFloat/4)
(In each case, if you have at least one, the compiler will convert the other to Floatto match.)
(在每种情况下,如果您至少有一个,编译器会将另一个转换Float为匹配。)
回答by Alex Wilson
Any of these help?
这些有帮助吗?
println(3.0/4.0)
println(3.toDouble/4.toDouble)
回答by Rick-777
For the primitive types (Int, Float, Double etc), Scala follows the rules in Java.
对于原始类型(Int、Float、Double 等),Scala 遵循 Java 中的规则。
If you write literal integers 3, 4, and so on, their expressions will remain as integers. So
如果您编写文字整数 3、4 等,它们的表达式将保持为整数。所以
println(3/4)
has an expression consisting of two integers so the result is an integer: 0.
有一个由两个整数组成的表达式,所以结果是一个整数:0。
If any of the values happens to be a Float instead of an Int, the whole expression widens to become a Float. The same is true for a Double. This widening happens almost completely automatically - you just have to give the compiler the hint of what you wanted, e.g.
如果任何值碰巧是 Float 而不是 Int,则整个表达式会扩展为 Float。Double 也是如此。这种扩大几乎完全自动发生 - 你只需要给编译器你想要的提示,例如
println(3f/4)
widens to Float and
扩大到浮动和
println(3.0/4)
widens to Double.
扩大到双倍。
When using these language features in real programs, always think about the loss of precision that can occur. You've already noticed the integer division causing the loss of the decimal fraction in that case. But there's a different case for floating point that is illustrated here:
在实际程序中使用这些语言功能时,请始终考虑可能发生的精度损失。在这种情况下,您已经注意到整数除法会导致小数丢失。但是这里说明了一种不同的浮点情况:
println(4f/3)
The actual result is held in a binary representation (using the IEEE754 standard) which has binary fractions, not decimal fractions, and so not able to hold 4f/3 without a small error.
实际结果保存在二进制表示中(使用 IEEE754 标准),它具有二进制分数,而不是十进制分数,因此无法在没有小错误的情况下保持 4f/3。
scala> println(100f/99f * 99)
99.99999
You might have expected it to print 100. Sometimes this doesn't matter and sometimes it does. One very common case where it matters a lot is when dealing with monetary values. The rule of thumb is ALWAYS AVOID float and double for money: use BigDecimal instead, or just hold the number of pence as a Long perhaps, or sometimes resort to Strings (which play nicely with BigDecimal provided they are not parsed as Double values on the way).
您可能期望它打印 100。有时这无关紧要,有时确实如此。一种非常重要的非常常见的情况是在处理货币价值时。经验法则是永远避免浮点数和双倍的钱:使用 BigDecimal 代替,或者只是将便士的数量保持为 Long,或者有时使用字符串(如果它们没有被解析为双精度值,则它们与 BigDecimal 配合得很好)大大地)。

