Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
Parse a number with commas in Kotlin

In Kotlin there is a shortcut for parsing an Int from a String:

"10".toInt()

However, if the number has a comma inside, e.g. "1,000".toInt(), it throws a NumberFormatException. I know I can use NumberFormat and Locale in Java to parse the number out. However, I wonder if there is a shortcut version for this in Kotlin.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Following the NumberFormat documentation, you can use NumberFormat to parse strings as well:

val numberFormat = NumberFormat.getNumberInstance(Locale.ENGLISH) as DecimalFormat
println(numberFormat.parse("1,000"))

>>> 1000

There is no shortcut in Kotlin. You have to specify the locale somewhere in order for Kotlin to know what the , means. It could be either a thousand separator - as in English - or a decimal point, as in German:

val numberFormat = NumberFormat.getNumberInstance(Locale.GERMAN) as DecimalFormat
println(numberFormat.parse("1,000"))

>>> 1
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda