Updated on 2026-08-14
This commit is contained in:
parent
a107f9299d
commit
13f139afa8
3 changed files with 63 additions and 2 deletions
|
|
@ -232,7 +232,7 @@ private fun BigDecimal.isLessThanThreshold() = this > BigDecimal.ZERO && this <
|
|||
* Returns amount with correct scale
|
||||
*/
|
||||
fun getFiatPriceAmountWithScale(value: BigDecimal): Pair<BigDecimal, Int> {
|
||||
return if (value < BigDecimal.ONE) {
|
||||
return if (value > BigDecimal.ZERO && value < BigDecimal.ONE) {
|
||||
val leadingZeroes = value.scale() - value.precision()
|
||||
val scale = leadingZeroes + FRACTIONAL_PART_LENGTH_AFTER_LEADING_ZEROES
|
||||
|
||||
|
|
|
|||
|
|
@ -294,4 +294,52 @@ internal class BigDecimalFiatFormatTest {
|
|||
Truth.assertThat(formatted)
|
||||
.isEqualTo("0.00000000000000000000123".addUsdSymbolLeft())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `price zero is formatted with default precision and does not crash`() {
|
||||
val testValue = BigDecimal.ZERO
|
||||
|
||||
val formatted = testValue.format {
|
||||
fiat(
|
||||
fiatCurrencyCode = usdCurrencyCode,
|
||||
fiatCurrencySymbol = usdSymbol,
|
||||
locale = testLocale,
|
||||
).price()
|
||||
}
|
||||
|
||||
Truth.assertThat(formatted)
|
||||
.isEqualTo("0.00".addUsdSymbolLeft())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `price negative integer keeps sign and does not crash`() {
|
||||
val testValue = BigDecimal("-500")
|
||||
|
||||
val formatted = testValue.format {
|
||||
fiat(
|
||||
fiatCurrencyCode = usdCurrencyCode,
|
||||
fiatCurrencySymbol = usdSymbol,
|
||||
locale = testLocale,
|
||||
).price()
|
||||
}
|
||||
|
||||
Truth.assertThat(formatted)
|
||||
.isEqualTo("-" + "500.00".addUsdSymbolLeft())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `price negative fractional keeps sign and does not crash`() {
|
||||
val testValue = BigDecimal("-0.5")
|
||||
|
||||
val formatted = testValue.format {
|
||||
fiat(
|
||||
fiatCurrencyCode = usdCurrencyCode,
|
||||
fiatCurrencySymbol = usdSymbol,
|
||||
locale = testLocale,
|
||||
).price()
|
||||
}
|
||||
|
||||
Truth.assertThat(formatted)
|
||||
.isEqualTo("-" + "0.50".addUsdSymbolLeft())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue