Skip to content

Commit

Permalink
Set DecimalFormatSymbols.minusSign for consistency across locales
Browse files Browse the repository at this point in the history
  • Loading branch information
Egorand committed Aug 11, 2023
1 parent 463b857 commit 3f2d510
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ public class CodeBlock private constructor(
val format = DecimalFormatSymbols().apply {
decimalSeparator = '.'
groupingSeparator = '_'
minusSign = '-'
}

val precision = if (o is Float || o is Double) o.toString().split(".").last().length else 0
Expand Down
13 changes: 13 additions & 0 deletions kotlinpoet/src/test/java/com/squareup/kotlinpoet/CodeBlockTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package com.squareup.kotlinpoet

import com.google.common.truth.Truth.assertThat
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import java.util.Locale
import kotlin.test.Test

class CodeBlockTest {
Expand Down Expand Up @@ -627,4 +628,16 @@ class CodeBlockTest {
assertThat(CodeBlock.of("%S", 10000.12345678901).toString()).isEqualTo("\"10000.12345678901\"")
assertThat(CodeBlock.of("%S", 1281.toShort()).toString()).isEqualTo("\"1281\"")
}

// https://github.com/square/kotlinpoet/issues/1657
@Test fun minusSignInSwedishLocale() {
val defaultLocale = Locale.getDefault()
Locale.setDefault(Locale.forLanguageTag("sv"))

val i = -42
val s = CodeBlock.of("val i = %L", i)
assertThat(s.toString()).isEqualTo("val i = -42")

Locale.setDefault(defaultLocale)
}
}

0 comments on commit 3f2d510

Please sign in to comment.