generated from hossain-khan/android-compose-app-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADDED] Text number formatter for 2 decimal formatting.
Added tests as well. The unit mess is getting out of hands - #60
- Loading branch information
1 parent
52215b1
commit 440e424
Showing
4 changed files
with
102 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
app/src/main/java/dev/hossain/weatheralert/util/TextFormatter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package dev.hossain.weatheralert.util | ||
|
||
import java.util.Locale | ||
|
||
/** | ||
* Formats 2.440923834343 to 2.44 cm | ||
*/ | ||
internal fun Float.formatUnit(unit: String): String = "%.2f %s".format(Locale.getDefault(), this, unit) | ||
|
||
/** | ||
* Formats 8.98237320 to 8.98 mm | ||
*/ | ||
internal fun Double.formatUnit(unit: String): String = "%.2f %s".format(Locale.getDefault(), this, unit) |
70 changes: 70 additions & 0 deletions
70
app/src/test/java/dev/hossain/weatheralert/util/TextFormatterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package dev.hossain.weatheralert.util | ||
|
||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
|
||
class TextFormatterTest { | ||
@Test | ||
fun formatUnit_handlesFloatValue() { | ||
val value = 2.440923834343f | ||
val unit = "cm" | ||
val expected = "2.44 cm" | ||
assertEquals(expected, value.formatUnit(unit)) | ||
} | ||
|
||
@Test | ||
fun formatUnit_handlesDoubleValue() { | ||
val value = 8.98237320 | ||
val unit = "mm" | ||
val expected = "8.98 mm" | ||
assertEquals(expected, value.formatUnit(unit)) | ||
} | ||
|
||
@Test | ||
fun formatUnit_handlesZeroFloatValue() { | ||
val value = 0.0f | ||
val unit = "cm" | ||
val expected = "0.00 cm" | ||
assertEquals(expected, value.formatUnit(unit)) | ||
} | ||
|
||
@Test | ||
fun formatUnit_handlesZeroDoubleValue() { | ||
val value = 0.0 | ||
val unit = "mm" | ||
val expected = "0.00 mm" | ||
assertEquals(expected, value.formatUnit(unit)) | ||
} | ||
|
||
@Test | ||
fun formatUnit_handlesNegativeFloatValue() { | ||
val value = -2.440923834343f | ||
val unit = "cm" | ||
val expected = "-2.44 cm" | ||
assertEquals(expected, value.formatUnit(unit)) | ||
} | ||
|
||
@Test | ||
fun formatUnit_handlesNegativeDoubleValue() { | ||
val value = -8.98237320 | ||
val unit = "mm" | ||
val expected = "-8.98 mm" | ||
assertEquals(expected, value.formatUnit(unit)) | ||
} | ||
|
||
@Test | ||
fun formatUnit_handlesLargeFloatValue() { | ||
val value = 123456.78923f | ||
val unit = "cm" | ||
val expected = "123456.79 cm" | ||
assertEquals(expected, value.formatUnit(unit)) | ||
} | ||
|
||
@Test | ||
fun formatUnit_handlesLargeDoubleValue() { | ||
val value = 987654.321 | ||
val unit = "mm" | ||
val expected = "987654.32 mm" | ||
assertEquals(expected, value.formatUnit(unit)) | ||
} | ||
} |