From e6207943587497741a6ccf018d09da9a8e1e150b Mon Sep 17 00:00:00 2001 From: Sad Ellie Date: Mon, 21 Nov 2022 11:11:16 +0400 Subject: [PATCH] Fixed engineering string formatter --- .../main/java/com/sadellie/unitto/screens/Utils.kt | 2 +- .../com/sadellie/unitto/screens/FormatterTest.kt | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/sadellie/unitto/screens/Utils.kt b/app/src/main/java/com/sadellie/unitto/screens/Utils.kt index b0dc3b6d..d33784e7 100644 --- a/app/src/main/java/com/sadellie/unitto/screens/Utils.kt +++ b/app/src/main/java/com/sadellie/unitto/screens/Utils.kt @@ -82,7 +82,7 @@ object Formatter { */ fun format(input: String): String { // Don't do anything to engineering string. - if (input.contains(KEY_E)) return formatNumber(input) + if (input.contains(KEY_E)) return input.replace(KEY_DOT, fractional) var output = input diff --git a/app/src/test/java/com/sadellie/unitto/screens/FormatterTest.kt b/app/src/test/java/com/sadellie/unitto/screens/FormatterTest.kt index 4c9b9b32..8be8c902 100644 --- a/app/src/test/java/com/sadellie/unitto/screens/FormatterTest.kt +++ b/app/src/test/java/com/sadellie/unitto/screens/FormatterTest.kt @@ -24,7 +24,8 @@ import org.junit.Test private val formatter = Formatter -private const val ENG_VALUE = "123.3E+21" +private const val ENG_VALUE = "123E+21" +private const val ENG_VALUE_FRACTIONAL = "123.3E+21" private const val COMPLETE_VALUE = "123456.789" private const val INCOMPLETE_VALUE = "123456." private const val NO_FRACTIONAL_VALUE = "123456" @@ -37,7 +38,8 @@ class FormatterTest { fun setSeparatorSpaces() { formatter.setSeparator(Separator.SPACES) assertEquals(".", formatter.fractional) - assertEquals("123.3E+21", formatter.format(ENG_VALUE)) + assertEquals("123E+21", formatter.format(ENG_VALUE)) + assertEquals("123.3E+21", formatter.format(ENG_VALUE_FRACTIONAL)) assertEquals("123 456.789", formatter.format(COMPLETE_VALUE)) assertEquals("123 456.", formatter.format(INCOMPLETE_VALUE)) assertEquals("123 456", formatter.format(NO_FRACTIONAL_VALUE)) @@ -49,7 +51,8 @@ class FormatterTest { fun setSeparatorComma() { formatter.setSeparator(Separator.COMMA) assertEquals(".", formatter.fractional) - assertEquals("123.3E+21", formatter.format(ENG_VALUE)) + assertEquals("123E+21", formatter.format(ENG_VALUE)) + assertEquals("123.3E+21", formatter.format(ENG_VALUE_FRACTIONAL)) assertEquals("123,456.789", formatter.format(COMPLETE_VALUE)) assertEquals("123,456.", formatter.format(INCOMPLETE_VALUE)) assertEquals("123,456", formatter.format(NO_FRACTIONAL_VALUE)) @@ -61,7 +64,8 @@ class FormatterTest { fun setSeparatorPeriod() { formatter.setSeparator(Separator.PERIOD) assertEquals(",", formatter.fractional) - assertEquals("123,3E+21", formatter.format(ENG_VALUE)) + assertEquals("123E+21", formatter.format(ENG_VALUE)) + assertEquals("123,3E+21", formatter.format(ENG_VALUE_FRACTIONAL)) assertEquals("123.456,789", formatter.format(COMPLETE_VALUE)) assertEquals("123.456,", formatter.format(INCOMPLETE_VALUE)) assertEquals("123.456", formatter.format(NO_FRACTIONAL_VALUE))