When I run the following program :
object Problem {
def main(args: Array[String]) = {
val v = 53.toString
val w = v(0).toInt
println(w)
}
}
it prints out 53, instead of what I would have expected, 5. Can someone help me understand why?
UPDATE: The same thing happens if I use charAt instead of the array syntax
53 is the ASCII value for the character '5'. Try 63.toString and you'll see 54 after v(0).toInt.
Use .asDigit to convert a Char to its Int value. In other words, '5'.toInt == 53 but '5'.asDigit == 5.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With