Skip to content

Commit

Permalink
Fix dollar() formatting crash in FR locale.
Browse files Browse the repository at this point in the history
  • Loading branch information
prashanDYDX committed Apr 17, 2024
1 parent d8b2274 commit 7b65f77
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package exchange.dydx.trading.common.formatter

import java.math.BigDecimal
import java.math.BigDecimal.ZERO
import java.math.RoundingMode
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
Expand Down Expand Up @@ -149,8 +150,8 @@ class DydxFormatter @Inject constructor() {
if (number == null) return null
val formattedNumber = localFormatted(number.abs(), digits)
return formattedNumber?.let {
val rawDouble = raw(number.toDouble(), digits)?.toDouble() ?: 0.0
if (rawDouble >= 0.0) {
val rounded = number.setScale(digits, RoundingMode.HALF_UP)
if (rounded >= BigDecimal.ZERO) {
"$$it"
} else {
"-$$it"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class DydxFormatterTests {
val number: Double,
val digits: Int = 2,
val expected: String,
val locale: Locale = Locale.US,
)

val testCases = listOf(
Expand All @@ -108,6 +109,14 @@ class DydxFormatterTests {
TestCase(number = 0.0, digits = 0, expected = "$0"),
TestCase(number = 0.0, digits = 1, expected = "$0.0"),
TestCase(number = 0.6, digits = 0, expected = "$1"),
TestCase(number = 1.0, digits = 2, expected = "$1.00", locale = Locale.FRANCE),
TestCase(number = 0.5, digits = 2, expected = "$0.50", locale = Locale.FRANCE),
TestCase(number = -0.25, digits = 2, expected = "-$0.25", locale = Locale.FRANCE),
TestCase(number = -0.0002, digits = 2, expected = "$0.00", locale = Locale.FRANCE),
TestCase(number = 0.0, digits = 2, expected = "$0.00", locale = Locale.FRANCE),
TestCase(number = 0.0, digits = 0, expected = "$0", locale = Locale.FRANCE),
TestCase(number = 0.0, digits = 1, expected = "$0.0", locale = Locale.FRANCE),
TestCase(number = 0.6, digits = 0, expected = "$1", locale = Locale.FRANCE),
)

testCases.forEach { testCase ->
Expand Down

0 comments on commit 7b65f77

Please sign in to comment.