Shalom Halbert
01/22/2022, 9:41 PMInt.MAX_VALUE
to a Double
? I’ve tried toDouble()
, but that outputs 2.147483647E9ephemient
01/22/2022, 10:32 PMInt.MAX_VALUE.toDouble()
is exact, you're just seeing Double.toString()
truncating to 9 digits of precision by defaultephemient
01/22/2022, 10:33 PM"%.10g".format(Int.MAX_VALUE.toDouble())
will show 10 digits of precision, which is enough for all digits of Int.MAX_VALUE
Shalom Halbert
01/22/2022, 10:57 PMInt.MAX_VALUE.toDouble()
equals 2.147483647E9 instead of 2147483647.0 is because that requires 11 digits? That’s confusing because the Kotlin docs say there can be “15-16” decimal digits. To clarify the situation, below are the cases referenced so far.
Int.MAX_VALUE // 2147483647
Int.MAX_VALUE.toDouble() // 2.147483647E9
"%.10g".format(Int.MAX_VALUE.toDouble()) // "2147483647"
ephemient
01/22/2022, 11:00 PMInt.MAX_VALUE.toDouble()
does not equal 2.147483647E9, that is just its string representationephemient
01/22/2022, 11:00 PMJoffrey
01/22/2022, 11:53 PMequals 2.147483647E9 instead of 2147483647.0@Shalom Halbert both of these are the same number. The first one is simply written in scientific notation (with E9 at the end meaning x10^9), while the other is in standard notation. It's just a matter of how you print the string representation of the number, as @ephemient pointed out. What is your issue with this output exactly? Is it because of the scientific notation, or is it because you believe the value is not the same?
ephemient
01/22/2022, 11:57 PMephemient
01/22/2022, 11:57 PMShalom Halbert
01/23/2022, 12:27 AMtoDouble()
converts the Int
into the same number, using scientific notation. Thank you for helping me realize that I was reading it wrongJoffrey
01/23/2022, 12:31 AMInt.toDouble()
is format-agnostic, it converts the int value into the same value of type Double
. The scientific notation only matters (or even exists) when you try to print the numberephemient
01/23/2022, 12:57 AMint.toDouble().toInt() == int // for all Int, but not all Long
double.toString().toDouble() == double // except NaN
Joffrey
01/23/2022, 1:00 AMInt.toDouble()
, of course when strings are involved it's all about format