By Rakete1111


2019-04-11 16:06:15 8 Comments

Consider:

val foo: Int = 1
foo.toDouble() // ok

val bar = 2.toUInt()
bar.toDouble() // error!

This doesn't make sense to me. Why wouldn't UInt have toDouble? (it also doesn't have .toFloat).

The docs say:

Every number type supports the following conversions:

  • toByte(): Byte
  • toShort(): Short
  • toInt(): Int
  • toLong(): Long
  • toFloat(): Float
  • toDouble(): Double
  • toChar(): Char

So it should be possible. The error I get is:

Error:(11, 4) Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
@InlineOnly public inline fun String.toDouble(): Double defined in kotlin.text

Is UInt not considered a number type? Or is it something else?

3 comments

@Andrew Churilo 2019-04-11 16:12:49

Is UInt not considered a number type?

Yes, it doesn't extend Number class.

Declaration of Int:

class Int : Number, Comparable<Int>

Declaration of UInt:

inline class UInt : Comparable<UInt>

Starting with Kotlin version 1.3.30 UInt has toFloat and toDouble methods.

@Rakete1111 2019-04-11 16:14:24

Thanks, that was surprising. Is there a way to convert a UInt to a Double in some other way?

@Andrew Churilo 2019-04-11 16:18:10

@Rakete1111 Try bar.toLong.toDouble()

@young 2019-04-12 10:01:43

Added support in latest version 1.3.30.

This release (More) brings support for more operations for unsigned types and arrays of unsigned types that mirror those for regular number types:

fun main() {
    val u1 = 2_147_483_649u
    val u2 = 4_000_000_000u
    println(u1.toDouble())
    println(minOf(u1, u2))

    val array: UIntArray = uintArrayOf(u1, u2)
    println(array.max())
    println(array.all { it > Int.MAX_VALUE.toUInt() })
}    

Note: UInt doesn't extend Number class.

/**
 * Converts this [UInt] value to [Double].
 *
 * The resulting `Double` value represents the same numerical value as this `UInt`.
 */
@kotlin.internal.InlineOnly
public inline fun toDouble(): Double = uintToDouble(data)

@Todd 2019-04-11 16:13:20

This appears to be coming in 1.3.30, according to this YouTrack request.

1.3.30 was just recently tagged and appears to be releasing very shortly.

Related Questions

Sponsored Content

0 Answered Questions

3 Answered Questions

[SOLVED] Kotlin - nullable receiver extension won't accept non-nullable equivalent

  • 2019-01-26 10:35:40
  • series0ne
  • 38 View
  • 0 Score
  • 3 Answer
  • Tags:   kotlin

1 Answered Questions

[SOLVED] Kotlin method references in place of lambda

  • 2018-04-23 14:08:53
  • lukeg
  • 341 View
  • 1 Score
  • 1 Answer
  • Tags:   methods kotlin

0 Answered Questions

Anko logger exception in calling

  • 2018-09-03 12:20:26
  • Александр Бабич
  • 51 View
  • 0 Score
  • 0 Answer
  • Tags:   android kotlin anko

1 Answered Questions

[SOLVED] Referencing list element inside of map in Kotlin

1 Answered Questions

[SOLVED] How to extract kotlin-react html into a method

  • 2017-12-28 10:01:56
  • auser
  • 217 View
  • 4 Score
  • 1 Answer
  • Tags:   reactjs kotlin

1 Answered Questions

[SOLVED] Type mismatch using inject()-function from Koin

1 Answered Questions

[SOLVED] How to get ResultSet string array?

  • 2017-11-30 17:58:03
  • Abe Miessler
  • 476 View
  • 0 Score
  • 1 Answer
  • Tags:   java kotlin crate

1 Answered Questions

[SOLVED] Smart-cast and comparison inside When Expression after 'is' type-check

  • 2017-11-05 00:26:55
  • Aria Pahlavan
  • 251 View
  • 3 Score
  • 1 Answer
  • Tags:   java kotlin

1 Answered Questions

[SOLVED] Generic Extension Property Receiver Type Mismatch

  • 2016-11-25 16:04:24
  • Daniel Douglas Soyka
  • 456 View
  • 0 Score
  • 1 Answer
  • Tags:   android kotlin

Sponsored Content