From 5090e22da0545a9908c0c0168a90b97f4f2eb45d Mon Sep 17 00:00:00 2001 From: DanielGronau Date: Wed, 5 Jun 2024 21:38:45 +0200 Subject: [PATCH 1/2] fix #1919 Small double values are set to zero in %L translation --- .../com/squareup/kotlinpoet/CodeBlock.kt | 7 +++- .../com/squareup/kotlinpoet/CodeBlockTest.kt | 37 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/CodeBlock.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/CodeBlock.kt index 17a7d881f..4e9f44b2c 100644 --- a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/CodeBlock.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/CodeBlock.kt @@ -22,6 +22,7 @@ import java.text.DecimalFormat import java.text.DecimalFormatSymbols import javax.lang.model.element.Element import javax.lang.model.type.TypeMirror +import kotlin.math.max import kotlin.reflect.KClass /** @@ -374,7 +375,11 @@ public class CodeBlock private constructor( minusSign = '-' } - val precision = if (o is Float || o is Double) o.toString().split(".").last().length else 0 + val precision = when(o) { + is Float -> max(o.toBigDecimal().stripTrailingZeros().scale(), 1) + is Double -> max(o.toBigDecimal().stripTrailingZeros().scale(), 1) + else -> 0 + } val pattern = when (o) { is Float, is Double -> "###,##0.0" + "#".repeat(precision - 1) diff --git a/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/CodeBlockTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/CodeBlockTest.kt index e28b3be68..400b4fc7c 100644 --- a/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/CodeBlockTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/CodeBlockTest.kt @@ -37,6 +37,43 @@ class CodeBlockTest { assertThat(a.toString()).isEqualTo("delicious taco") } + @Test fun doublePrecision() { + val doubles = listOf( + 12345678900000.0 to "12_345_678_900_000.0", + 12345678900000.07 to "12_345_678_900_000.07", + 123456.0 to "123_456.0", + 1234.5678 to "1_234.5678", + 12.345678 to "12.345678", + 0.12345678 to "0.12345678", + 0.0001 to "0.0001", + 0.00001 to "0.00001", + 0.000001 to "0.000001", + 0.0000001 to "0.0000001", + ) + for((d, expected) in doubles) { + val a = CodeBlock.of("number %L", d) + assertThat(a.toString()).isEqualTo("number $expected") + } + } + + @Test fun floatPrecision() { + val floats = listOf( + 12345678.0f to "12_345_678.0", + 123456.0f to "123_456.0", + 1234.567f to "1_234.567", + 12.34567f to "12.34567", + 0.1234567f to "0.1234567", + 0.0001f to "0.0001", + 0.00001f to "0.00001", + 0.000001f to "0.000001", + 0.0000001f to "0.0000001", + ) + for((f, expected) in floats) { + val a = CodeBlock.of("number %L", f) + assertThat(a.toString()).isEqualTo("number $expected") + } + } + @Test fun percentEscapeCannotBeIndexed() { assertThrows { CodeBlock.builder().add("%1%", "taco").build() From 227742996fbc446050b41893934deeb2dc8683d6 Mon Sep 17 00:00:00 2001 From: DanielGronau Date: Thu, 6 Jun 2024 21:20:54 +0200 Subject: [PATCH 2/2] changelog, spotless --- docs/changelog.md | 2 ++ .../commonMain/kotlin/com/squareup/kotlinpoet/CodeBlock.kt | 2 +- .../kotlin/com/squareup/kotlinpoet/CodeBlockTest.kt | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 668118ad1..c3185e3d9 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -3,6 +3,8 @@ Change Log ## Unreleased +* Fix: Small double and float values were set to 0.0 in %L translation (#1919) + ## Version 1.17.0 Thanks to [@jisungbin][jisungbin], [@hfhbd][hfhbd], [@evant][evant], [@sgjesse][sgjesse], [@sebek64][sebek64] for diff --git a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/CodeBlock.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/CodeBlock.kt index 4e9f44b2c..2f6287375 100644 --- a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/CodeBlock.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/CodeBlock.kt @@ -375,7 +375,7 @@ public class CodeBlock private constructor( minusSign = '-' } - val precision = when(o) { + val precision = when (o) { is Float -> max(o.toBigDecimal().stripTrailingZeros().scale(), 1) is Double -> max(o.toBigDecimal().stripTrailingZeros().scale(), 1) else -> 0 diff --git a/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/CodeBlockTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/CodeBlockTest.kt index 400b4fc7c..5d1a3df4e 100644 --- a/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/CodeBlockTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/CodeBlockTest.kt @@ -50,7 +50,7 @@ class CodeBlockTest { 0.000001 to "0.000001", 0.0000001 to "0.0000001", ) - for((d, expected) in doubles) { + for ((d, expected) in doubles) { val a = CodeBlock.of("number %L", d) assertThat(a.toString()).isEqualTo("number $expected") } @@ -68,7 +68,7 @@ class CodeBlockTest { 0.000001f to "0.000001", 0.0000001f to "0.0000001", ) - for((f, expected) in floats) { + for ((f, expected) in floats) { val a = CodeBlock.of("number %L", f) assertThat(a.toString()).isEqualTo("number $expected") }