Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Fix negative zero issue on balance (#2715)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedmolawale authored Sep 27, 2023
1 parent f362985 commit 7df0ad8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import com.ivy.design.l0_system.UI
import com.ivy.design.l0_system.style
import com.ivy.legacy.IvyWalletComponentPreview
import com.ivy.legacy.utils.decimalPartFormatted
import com.ivy.legacy.utils.integerPartFormatted
import com.ivy.legacy.utils.shortenAmount
import com.ivy.legacy.utils.shouldShortAmount
import java.text.DecimalFormat

@Deprecated("Old design system. Use `:ivy-design` and Material3")
@Composable
Expand Down Expand Up @@ -121,12 +121,10 @@ fun BalanceRow(
Spacer(Modifier.width(spacerCurrency))
}

val balancePrecise = balance.toBigDecimal()

val integerPartFormatted = if (shortAmount) {
shortenAmount(balance)
} else {
DecimalFormat("###,###").format(balancePrecise.toInt())
integerPartFormatted(balance)
}
Text(
text = when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,17 @@ fun formatInputAmount(

return null
}

/**
toInt on numbers in the range (-1.0, 0.0) (exclusive of boundaries) will produce a positive int 0
So, this function append negative sign in that case
*/
fun integerPartFormatted(value: Double): String {
val preciseValue = value.toBigDecimal()
val formattedValue = DecimalFormat("###,###").format(preciseValue.toInt())
return if (value > -1.0 && value < 0.0) {
"-$formattedValue"
} else {
formattedValue
}
}

0 comments on commit 7df0ad8

Please sign in to comment.