digitToInt

Common
JVM
JS
Native
1.4
@ExperimentalStdlibApi fun Char.digitToInt(): Int

Returns the numeric value of the decimal digit that this Char represents. Throws an exception if this Char is not a valid decimal digit.

A Char is considered to represent a decimal digit if the Char is one of the ASCII decimal digits '0' through '9'. In this case, this.code - '0'.code is returned.

import java.util.*
import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
println('5'.digitToInt()) // 5
println('3'.digitToInt(radix = 8)) // 3
println('A'.digitToInt(radix = 16)) // 10
println('k'.digitToInt(radix = 36)) // 20

// radix argument should be in 2..36
// '0'.digitToInt(radix = 1) //  will fail
// '1'.digitToInt(radix = 100) //  will fail
// only 0 and 1 digits are valid for binary numbers
// '5'.digitToInt(radix = 2) //  will fail
// radix = 10 is used by default
// 'A'.digitToInt() //  will fail
// symbol '+' is not a digit in any radix
// '+'.digitToInt() //  will fail
// Only Latin letters are valid for digits greater than 9.
// 'β'.digitToInt(radix = 36) //  will fail
//sampleEnd
}
Common
JVM
JS
Native
1.4
@ExperimentalStdlibApi fun Char.digitToInt(radix: Int): Int

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

A Char is considered to represent a digit in the specified radix if at least one of the following is true:

  • The Char is one of the ASCII decimal digits '0' through '9' and its code is less than radix + '0'.code. In this case, this.code - '0'.code is returned.
  • The Char is one of the uppercase Latin letters 'A' through 'Z' and its code is less than radix + 'A'.code - 10. In this case, this.code - 'A'.code + 10 is returned.
  • The Char is one of the lowercase Latin letters 'a' through 'z' and its code is less than radix + 'a'.code - 10. In this case, this.code - 'a'.code + 10 is returned.
import java.util.*
import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
println('5'.digitToInt()) // 5
println('3'.digitToInt(radix = 8)) // 3
println('A'.digitToInt(radix = 16)) // 10
println('k'.digitToInt(radix = 36)) // 20

// radix argument should be in 2..36
// '0'.digitToInt(radix = 1) //  will fail
// '1'.digitToInt(radix = 100) //  will fail
// only 0 and 1 digits are valid for binary numbers
// '5'.digitToInt(radix = 2) //  will fail
// radix = 10 is used by default
// 'A'.digitToInt() //  will fail
// symbol '+' is not a digit in any radix
// '+'.digitToInt() //  will fail
// Only Latin letters are valid for digits greater than 9.
// 'β'.digitToInt(radix = 36) //  will fail
//sampleEnd
}