From 95e8d43a4c020ac3bdb3979419535e82a10bee43 Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Wed, 12 Aug 2020 21:09:54 +0200 Subject: [PATCH] Fix scientific notation parsing, when string doesn't have a decimal point, like JS Double.MIN_VALUE --- .../com/ionspin/kotlin/bignum/decimal/BigDecimal.kt | 8 +++++++- .../kotlin/bignum/decimal/BigDecimalCreationTest.kt | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimal.kt b/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimal.kt index e2e7ebb4..7e14b0ad 100644 --- a/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimal.kt +++ b/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimal.kt @@ -629,7 +629,13 @@ class BigDecimal private constructor( } if (floatingPointString.contains('E') || floatingPointString.contains('e')) { // Sci notation - val split = floatingPointString.split('.') + val split = if (floatingPointString.contains('.').not()) { + // As is case with JS Double.MIN_VALUE + val splitAroundE = floatingPointString.split('E', 'e') + listOf(splitAroundE[0], "0E" + splitAroundE[1]) + } else { + floatingPointString.split('.') + } when (split.size) { 2 -> { val signPresent = (floatingPointString[0] == '-' || floatingPointString[0] == '+') diff --git a/bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimalCreationTest.kt b/bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimalCreationTest.kt index 84e71b46..38b1bc88 100644 --- a/bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimalCreationTest.kt +++ b/bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimalCreationTest.kt @@ -104,6 +104,12 @@ class BigDecimalCreationTest { val b = BigDecimal.fromIntWithExponent(-123123, 2) a == b } + + assertTrue { + val a = BigDecimal.parseStringWithMode("5E-324") + val b = BigDecimal.fromIntWithExponent(5, -324) + a == b + } } @Test