Issue
I'm creating a currency application but some of values are like "194.23564" or "1187.7594" so i want to show the user before the "." sign values. How can i make this with Kotlin ?
Solution
There is no need for data type conversion before the extraction of the integer part.
You can use substringBefore()
:
val number = "194.23564"
val intPart = number.substringBefore(".")
If you want the result as an integer number you can use now toIntOrNull()
, instead of toInt()
, so to avoid an exception in case the initial string has no integer part (like ".015"
):
val intPart = number.substringBefore(".").toIntOrNull()
Answered By - forpas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.