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 Subaccount transfer for France #39

Merged
merged 3 commits into from
Mar 22, 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
Expand Up @@ -45,12 +45,6 @@ class DydxFormatter @Inject constructor() {
SimpleDateFormat("yyyy-MM-dd HH:mm:ss", locale)
}

private val rawFormatter: NumberFormat by lazy {
NumberFormat.getNumberInstance().apply {
isGroupingUsed = false
}
}

fun dollarVolume(number: Double?, digits: Int = 2): String? {
return dollarVolume(number?.toBigDecimal(), digits)
}
Expand All @@ -76,15 +70,15 @@ class DydxFormatter @Inject constructor() {
fun condensed(number: BigDecimal?, digits: Int = 4): String? {
if (number != null) {
val postfix = arrayOf("", "K", "M", "B", "T")
var value = number.toDouble()
var value = number.toDouble().absoluteValue
var index = 0
while (value > 1000.0 && index < (postfix.size - 1)) {
value /= 1000.0
index++
}
val number = BigDecimal(value).setScale(digits, RoundingMode.HALF_UP)
val numberString = formatBigDecimal(number)
return "$numberString${postfix[index]}"
val formatted = BigDecimal(value).setScale(digits, RoundingMode.HALF_UP)
val numberString = formatBigDecimal(formatted)
return if (number >= BigDecimal.ZERO) "$numberString${postfix[index]}" else "-$numberString${postfix[index]}"
}
return null
}
Expand Down Expand Up @@ -112,7 +106,14 @@ class DydxFormatter @Inject constructor() {
val decimalFormat = NumberFormat.getInstance(locale) as? DecimalFormat
decimalFormat?.minimumFractionDigits = maxOf(digits, 0)
decimalFormat?.maximumFractionDigits = maxOf(digits, 0)
return decimalFormat?.format(number)

val formatted = decimalFormat?.format(number)
val parsed = decimalFormat?.parse(formatted)
if (parsed?.toDouble() == 0.0) { // handle -0.0
return decimalFormat.format(0.0)
} else {
return formatted
}
}
return null
}
Expand All @@ -133,36 +134,23 @@ class DydxFormatter @Inject constructor() {
}

fun dollar(number: Double?, size: String? = null): String? {
if (number != null) {
return dollar(number = number.toBigDecimal(), size = size)
}
return null
return dollar(number = number?.toBigDecimal(), size = size)
}

fun dollar(number: BigDecimal?, size: String? = null): String? {
if (number == null) return null
val formattedNumber = localFormatted(number.abs(), size ?: "0.01")
return formattedNumber?.let {
if (number >= BigDecimal.ZERO) {
"$$it"
} else {
"-$$it"
}
}
return dollar(number, digits = digits(size ?: "0.01"))
}

fun dollar(number: Double?, digits: Int): String? {
if (number != null) {
return dollar(number = number.toBigDecimal(), digits = digits)
}
return null
return dollar(number = number?.toBigDecimal(), digits = digits)
}

fun dollar(number: BigDecimal?, digits: Int): String? {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TOL: do we need a digits param for USD? Shouldn't it always be 2 decimal places?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... I don't remember a case off the top of my head. But the decimals are configured by markets, so there might be some usecases.

if (number == null) return null
val formattedNumber = localFormatted(number.abs(), digits)
return formattedNumber?.let {
if (number >= BigDecimal.ZERO) {
val rawDouble = raw(number.toDouble(), digits)?.toDouble() ?: 0.0
if (rawDouble >= 0.0) {
"$$it"
} else {
"-$$it"
Expand All @@ -171,10 +159,7 @@ class DydxFormatter @Inject constructor() {
}

fun percent(number: Double?, digits: Int, minDigits: Int? = null): String? {
if (number != null) {
return percent(number = number.toBigDecimal(), digits = digits, minDigits = minDigits)
}
return null
return percent(number = number?.toBigDecimal(), digits = digits, minDigits = minDigits)
}

fun percent(number: BigDecimal?, digits: Int, minDigits: Int? = null): String? {
Expand Down Expand Up @@ -279,14 +264,33 @@ class DydxFormatter @Inject constructor() {
}
}

fun raw(number: Double?, digits: Int? = null): String? {
/*
xxxxx.yyyyy
*/
fun decimalLocaleAgnostic(number: Double?, digits: Int): String? {
return raw(number = number, digits = digits, locale = Locale.US)
}

/*
xxxxxx,yyyyy or xxxxx.yyyyy
*/
fun raw(number: Double?, digits: Int? = null, locale: Locale = Locale.getDefault()): String? {
return number?.let { value ->
if (value.isFinite()) {
if (digits != null) {
rawFormatter.minimumFractionDigits = maxOf(digits, 0)
rawFormatter.maximumFractionDigits = maxOf(digits, 0)
rawFormatter.roundingMode = RoundingMode.HALF_UP
rawFormatter.format(number)
val rawFormatter = DecimalFormat.getInstance(locale).apply {
minimumFractionDigits = maxOf(digits, 0)
maximumFractionDigits = maxOf(digits, 0)
roundingMode = RoundingMode.HALF_UP
isGroupingUsed = false
}
val formatted = rawFormatter.format(number)
val number = rawFormatter.parse(formatted)
if (number.toDouble() == 0.0) { // handle -0.0
rawFormatter.format(0.0)
} else {
formatted
}
} else {
BigDecimal(number).toPlainString()
}
Expand Down
16 changes: 0 additions & 16 deletions v4/common/src/test/java/exchange/dydx/common/ExampleUnitTest.kt

This file was deleted.

Loading
Loading