Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dollar() formatting crash in FR locale. #74

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,9 +109,18 @@ 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 ->
formatter.locale = testCase.locale
val formatted = formatter.dollar(number = testCase.number, digits = testCase.digits)
assert(formatted == testCase.expected) { "Test case: $testCase, formatted: $formatted" }
}
Expand Down
Loading