Skip to content

Conversation

mahozad
Copy link
Contributor

@mahozad mahozad commented Sep 14, 2025

Java Integer::parseInt and Kotlin String.toInt and other String to non-float functions accept non-ASCII (aka non-English aka non-Western-Arabic) digits in a String too. For example, Eastern Arabic digits:

println("12۳".toByte())        // 123
println("12۳۴".toShort())      // 1234
println("12۳۴۵".toIntOrNull()) // 12345
println("12۳۴۵6".toUInt())     // 123456
println("12۳۴۵67".toLong())    // 1234567

This is a good and expected behavior in my opinion. I don't know if Kotlin implementation of these functions for non-JVM targets also behave the same way.

Alos, Java Character::isDigit and Kotlin Char.isDigit return true for non-ASCII numerals too. Same for "\\p{javaDigit}" or "\\p{Nd}" regexes in both Java and Kotlin. See this code snippet. They all match a few hundred types of digit chars in JDK 24.

I have updated the docs to hopefully make it just a bit more clear that 0-9 are just one set of chars that are valid as digits. I don't know whether all those functions updated in this commit (especially non-JVM or expect ones) accept non-English digits too. If not, then my update for docs of those should be reverted and I hope their implementations is updated to make them behave the same way as on JVM implementation.

Another thing is, it seems most of those functions lack unit tests for non-ASCII digits to clarify their behavior. Found a weird bug:

println("123۴۵۶.789۳۲۱".toBigDecimal())       // 123456.789321
// Exactly the same string as above
println("123۴۵۶.789۳۲۱".toBigDecimalOrNull()) // null

And, BigDecimal allows non-ASCII digits in the string, but String.toDouble and variants throw exception.

Related:

Java Integer::parseInt and Kotlin String.toInt and the like accept non-ASCII (more accurately non-Western-Arabic) digits too. Make the documentation a little more clear about this.
@fzhinkin
Copy link
Contributor

fzhinkin commented Sep 15, 2025

With toBigDecimal, it's clearly a bug.
Filed https://youtrack.jetbrains.com/issue/KT-81043

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants