'How can i show the value before the "." sign?

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 1:[1]

Other than suggested, I would not convert to Float. This is susceptible to rounding errors and may not return the value before the decimal point.

Example:

val num = "0.99999999"
println(num.toFloat().toInt())  // gives 1

Instead, split the string at the decimal point:

val num = "0.99999999"
val split = num.split('.')
println(split[0])  // gives 0

A nice side effect of this implementation is that it even works for integral numbers without a decimal point. If you need the result as an Int, simply call split[0].toInt().

Solution 2:[2]

There is no need to use float in this case. If you want to get the value before ".", you need to use int instead of float. When you use float you will get value in points, but when you use int, you will get the value before ","

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TheOperator
Solution 2 Chris Catignani