digitToChar

fun Int.digitToChar(): Char

Returns the Char that represents this decimal digit. Throws an exception if this value is not in the range 0..9.

If this value is in 0..9, the decimal digit Char with code '0'.code + this is returned.

Since Kotlin

1.5

Samples

import samples.*
import java.util.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   assertPrints(5.digitToChar(), "5")
assertPrints(3.digitToChar(radix = 8), "3")
assertPrints(10.digitToChar(radix = 16), "A")
assertPrints(20.digitToChar(radix = 36), "K")

// radix argument should be in 2..36
assertFails { 0.digitToChar(radix = 1) }
assertFails { 1.digitToChar(radix = 100) }
// only 0 and 1 digits are valid for binary numbers
assertFails { 5.digitToChar(radix = 2) }
// radix = 10 is used by default
assertFails { 10.digitToChar() }
// a negative integer is not a digit in any radix
assertFails { (-1).digitToChar() } 
   //sampleEnd
}

fun Int.digitToChar(radix: Int): Char

Returns the Char that represents this numeric digit value in the specified radix. Throws an exception if the radix is not in the range 2..36 or if this value is not in the range 0 until radix.

If this value is less than 10, the decimal digit Char with code '0'.code + this is returned. Otherwise, the uppercase Latin letter with code 'A'.code + this - 10 is returned.

Since Kotlin

1.5

Samples

import samples.*
import java.util.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   assertPrints(5.digitToChar(), "5")
assertPrints(3.digitToChar(radix = 8), "3")
assertPrints(10.digitToChar(radix = 16), "A")
assertPrints(20.digitToChar(radix = 36), "K")

// radix argument should be in 2..36
assertFails { 0.digitToChar(radix = 1) }
assertFails { 1.digitToChar(radix = 100) }
// only 0 and 1 digits are valid for binary numbers
assertFails { 5.digitToChar(radix = 2) }
// radix = 10 is used by default
assertFails { 10.digitToChar() }
// a negative integer is not a digit in any radix
assertFails { (-1).digitToChar() } 
   //sampleEnd
}