From 65ad0e998242e1258204b3e0670d5ea40810f467 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Wed, 26 May 2021 14:18:47 -0700 Subject: [PATCH] Added minFractionDigits parameter to toCurrency. --- README.md | 7 +++++-- .../net/thauvin/erik/crypto/CryptoPrice.kt | 6 +++--- .../thauvin/erik/crypto/CryptoPriceTest.kt | 21 +++++++++++-------- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 1ba3489..1203e64 100644 --- a/README.md +++ b/README.md @@ -50,8 +50,11 @@ The parameter names match the [Coinbase API](https://developers.coinbase.com/api To display the amount as a fomatted currency use the `toCurrency` function: ```kotlin -val price = CryptoPrice("BTC", "EUR", 12345.67.toBigDecimal()) -println(price.toCurrency()) // will print €12,345.67 +val euro = CryptoPrice("BTC", "EUR", 12345.67.toBigDecimal()) +println(euro.toCurrency()) // will print: €12,345.67 + +val krone = CryptoPrice("BTC", "DKK", 12345.67.toBigDecimal()) +println(krone.toCurrency(Locale("da", "DK"))) // will print: 12.345,67 kr. ``` ### Extending diff --git a/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt b/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt index 7248ff7..168a3ae 100644 --- a/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt +++ b/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt @@ -70,7 +70,7 @@ open class CryptoPrice(val base: String, val currency: String, val amount: BigDe throw CryptoException(message = "Missing price data.") } } catch (e: NumberFormatException) { - throw CryptoException(message = "Could not convert amount to number.", cause = e) + throw CryptoException(message = "Could not convert amount to number.", cause = e) } catch (e: JSONException) { throw CryptoException(message = "Could not parse price data.", cause = e) } @@ -143,10 +143,10 @@ open class CryptoPrice(val base: String, val currency: String, val amount: BigDe */ @JvmOverloads @Throws(IllegalArgumentException::class) - fun toCurrency(locale: Locale = Locale.getDefault(Locale.Category.FORMAT)): String { + fun toCurrency(locale: Locale = Locale.getDefault(Locale.Category.FORMAT), minFractionDigits: Int = 2): String { return NumberFormat.getCurrencyInstance(locale).let { it.setCurrency(Currency.getInstance(currency)) - it.setMinimumFractionDigits(2) + it.setMinimumFractionDigits(minFractionDigits) it.format(amount) } } diff --git a/src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt b/src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt index 0a228dc..bbf6166 100644 --- a/src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt +++ b/src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt @@ -85,35 +85,38 @@ class CryptoPriceTest { @Test @Throws(IllegalArgumentException::class) fun testToCurrency() { - val d = 12345.67.toBigDecimal() + val d = 12345.60.toBigDecimal() val usd = CryptoPrice("BTC", "USD", d) - assertEquals("$12,345.67", usd.toCurrency(), "EUR format") + assertEquals("$12,345.60", usd.toCurrency(), "EUR format") val eur = CryptoPrice("BTC", "EUR", d) - assertEquals("€12,345.67", eur.toCurrency(), "EUR format") + assertEquals("€12,345.60", eur.toCurrency(), "EUR format") val gbp = CryptoPrice("ETH", "GBP", d) - assertEquals("£12,345.67", gbp.toCurrency(), "GBP format") + assertEquals("£12,345.60", gbp.toCurrency(), "GBP format") val aud = CryptoPrice("BTC", "AUD", d) - assertEquals("A$12,345.67", aud.toCurrency(), "AUD format") + assertEquals("A$12,345.60", aud.toCurrency(), "AUD format") val dk = CryptoPrice("BTC", "DKK", d) - assertEquals("12.345,67 kr.", dk.toCurrency(Locale("da", "DK")), "EUR-DKK format") + assertEquals("12.345,60 kr.", dk.toCurrency(Locale("da", "DK")), "EUR-DKK format") val jp = CryptoPrice("BTC", "JPY", d) - assertEquals("¥12,345.67", jp.toCurrency(Locale.JAPAN), "EUR-JPY format") + assertEquals("¥12,345.60", jp.toCurrency(Locale.JAPAN), "EUR-JPY format") + + assertEquals("$12,345.6000", usd.toCurrency(minFractionDigits = 4), "minFractionDigits = 4") + assertEquals("$12,345.6", usd.toCurrency(minFractionDigits = 0), "minFractionDigits = 0") } @Test @Throws(CryptoException::class) fun testToPrice() { - val d = "57515.69" + val d = "57515.60" val json = "{\"data\":{\"base\":\"BTC\",\"currency\":\"USD\",\"amount\":\"$d\"}}" val price = json.toPrice() assertEquals("BTC", price.base, "base is BTC") assertEquals("USD", price.currency, "currency is USD") - assertEquals(d, price.amount.toString(), "amount is 57515.69") + assertEquals(d, price.amount.toString(), "amount is 57515.60") assertFailsWith( message = "double conversion did not fail",