Convert String to Float or Int in KOTLIN

I need to convert app version to Int or Float for comparison. I am using the following syntax, but it throws java.lang.NumberFormatException.

val currentVersion = BuildConfig.VERSION_NAME.toFloat()

Edit: What if I need to convert "2.2" to float?

App version comparison can be very straightforward, like comparing versions 1.0 and 2.0.

But sometimes it can become complicated if versions to compare are 1.0.2-rc2 and 1.0.2-rc3 or 2.0.0-beta and 2.0.0.In this situation, you cannot just convert your versionName to Int or Float type. Which is not future proof, by the way, cause most likely, 1.19 will be considered as an older version than 1.2.

In order to make this comparison easy, you can check this library - Version Compare (github). (Alternative - Semver4j) I think it will handle most of the cases.

Also, you can read a bit more about versioning in this post.

Convert String to Float or Int in KOTLIN, Parses the string as a Float number and returns the result. Exceptions. NumberFormatException - if the string is not a valid representation of a number. Kotlin convert String to Float In the following example, we will use: toFloat () to parse the string to a Float, NumberFormatException is thrown if the string is not a valid representation of a Float. toFloatOrNull () to convert the string to a Float, return a null if the string is not a valid representation of a Float.

Generally a version name is written as follow : "1.0.0" ( of course you can parse the string and then get 3 numbers : "1", "0" and "0" ) so you cannot convert it into a number format. Otherwhise you could use VERSION_CODE it will return a number

toFloat, Kotlin Convert String to Int Example. We are going to test the following examples: The toInt() method helps to parse the string to an Int� Kotlin – String to Integer – String.toInt () Convert String to Integer in Kotlin To convert a string to integer in Kotlin, use String.toInt () or Integer.parseInt () method. If the string can be converted to a valid integer, either of the methods returns int value.

If you got NumberFormatException it means that the string is not a valid representation of a number.

Your BuildConfig.VERSION_CODE is a valid Int, can you use it?

How to Convert String to Int, Float, Double, Long in Kotlin, In this example we are converting a long type to int type where the value of long type is outside the range of integer type. fun main(args : Array<String>)� Parses the string as a Float number and returns the result.. Exceptions. NumberFormatException - if the string is not a valid representation of a number.

Kotlin Type Casting with examples, Kotlin makes it really easy to parse String into other data types, such as Long, Integer, or Double.In JAVA, Long.parseLong(), or the Long. This is the easiest and the safest method to convert one string to number in Kotlin. Using toInt() : toInt() method converts the string to an integer. But if the string is not a valid integer, it throws one NumberFormatException. So, if you want to use this method, always make sure to wrap it inside a try-catch block. It has two variants :

Parsing String to Long, Double, or Int, //String to Float Conversion val variable_name = "string".toFloat() //Int and Double to Float Conversion val variable_name = int_variable.toFloat()� fun toFloat(): Float Converts this Int value to Float. The resulting value is the closest Float to this Int value. In case when this Int value is exactly between two Float s, the one with zero at least significant bit of mantissa is selected.

Float Data type Usage and Type Conversion in Kotlin, In this article, you will learn about type conversion; how to convert a variable of and Int to Char; String to Long, and Long to String Conversion; String to Array,� For Common, JVM, JS. Converts this Float value to Int.. The fractional part, if any, is rounded down towards zero. Returns zero if this Float value is NaN, Int.MIN_VALUE if it's less than Int.MIN_VALUE, Int.MAX_VALUE if it's bigger than Int.MAX_VALUE.

Comments
  • Use VERSION_CODE instead of VERSION_NAME
  • use VERSION_CODE ofr comparisons
  • Please check edit.
  • Why not you try "2.2".toFloat() and check? It returns 2.2
  • Yes, it returns 2.2, but my app version had 3 decimals which was causing the confusion. @Demigod gave the required answer. Thanks for the help.
  • Thanks @Demigod. Your explanation gives the required direction I need to proceed with app versioning.
  • Yeah, we can do that but lets make this simple, how can i convert "2.20" in String to float in kotlin?
  • Be sure that your string can be parse into Float format ( it will be 2.20 and not 2.20.x ) then you can call the toFloat() method. Surround your code with a try/catch to check the format of your string
  • Yes, that helps too, but @Demigod gave new direction for app versioning. Thanks for your help.