https://kotlinlang.org logo
Title
b

bj0

11/08/2017, 5:47 PM
if you get
NumberFormatException
than your string probably isn't able to be represented as a
Long
k

Kevin Janvier Chinabalire

11/08/2017, 5:52 PM
val numb = "1000.0"
    val amount : Int = numb.toInt()
    println("amonnn $" +amount.toString())
that wat am having am getting
NumberFormatException.forInputString
b

bj0

11/08/2017, 5:55 PM
because it's a floating point number
try
.toFloat()
or
.toDouble()
m

msink

11/08/2017, 6:10 PM
fun String.takeInt() : Int {
    val str = takeWhile { it.isDigit() }
    return if (str.isEmpty()) 0 else str.toInt()
}
b

bj0

11/08/2017, 6:51 PM
or just
.toDouble().toInt()
m

msink

11/08/2017, 7:23 PM
For strings like "1200MHz" :)
b

bj0

11/08/2017, 8:12 PM
what about "12,300"
k

Kevin Janvier Chinabalire

11/08/2017, 8:22 PM
thanks ..it worked
toDouble().toInt()