Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/ktextension/NumberExtensions.kt

29 lines
527 B
Kotlin
Raw Normal View History

2016-07-30 02:08:57 +02:00
package org.mariotaku.ktextension
/**
* Created by mariotaku on 16/7/30.
*/
fun String?.toLong(def: Long): Long {
2016-07-30 02:08:57 +02:00
try {
return this?.toLong() ?: def
2016-07-30 02:08:57 +02:00
} catch (e: NumberFormatException) {
return def
}
2016-08-29 13:03:55 +02:00
}
fun String?.toInt(def: Int): Int {
2016-12-03 16:45:13 +01:00
try {
return this?.toInt() ?: def
2016-12-03 16:45:13 +01:00
} catch (e: NumberFormatException) {
return def
}
}
2016-08-29 13:03:55 +02:00
fun String.toDoubleOrNull(): Double? {
try {
return toDouble()
} catch (e: NumberFormatException) {
return null
}
2016-07-30 02:08:57 +02:00
}