Added minFractionDigits parameter to toCurrency.
This commit is contained in:
parent
3f9ac5f342
commit
65ad0e9982
3 changed files with 20 additions and 14 deletions
|
@ -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:
|
To display the amount as a fomatted currency use the `toCurrency` function:
|
||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
val price = CryptoPrice("BTC", "EUR", 12345.67.toBigDecimal())
|
val euro = CryptoPrice("BTC", "EUR", 12345.67.toBigDecimal())
|
||||||
println(price.toCurrency()) // will print €12,345.67
|
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
|
### Extending
|
||||||
|
|
||||||
|
|
|
@ -70,7 +70,7 @@ open class CryptoPrice(val base: String, val currency: String, val amount: BigDe
|
||||||
throw CryptoException(message = "Missing price data.")
|
throw CryptoException(message = "Missing price data.")
|
||||||
}
|
}
|
||||||
} catch (e: NumberFormatException) {
|
} 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) {
|
} catch (e: JSONException) {
|
||||||
throw CryptoException(message = "Could not parse price data.", cause = e)
|
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
|
@JvmOverloads
|
||||||
@Throws(IllegalArgumentException::class)
|
@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 {
|
return NumberFormat.getCurrencyInstance(locale).let {
|
||||||
it.setCurrency(Currency.getInstance(currency))
|
it.setCurrency(Currency.getInstance(currency))
|
||||||
it.setMinimumFractionDigits(2)
|
it.setMinimumFractionDigits(minFractionDigits)
|
||||||
it.format(amount)
|
it.format(amount)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -85,35 +85,38 @@ class CryptoPriceTest {
|
||||||
@Test
|
@Test
|
||||||
@Throws(IllegalArgumentException::class)
|
@Throws(IllegalArgumentException::class)
|
||||||
fun testToCurrency() {
|
fun testToCurrency() {
|
||||||
val d = 12345.67.toBigDecimal()
|
val d = 12345.60.toBigDecimal()
|
||||||
val usd = CryptoPrice("BTC", "USD", d)
|
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)
|
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)
|
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)
|
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)
|
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)
|
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
|
@Test
|
||||||
@Throws(CryptoException::class)
|
@Throws(CryptoException::class)
|
||||||
fun testToPrice() {
|
fun testToPrice() {
|
||||||
val d = "57515.69"
|
val d = "57515.60"
|
||||||
val json = "{\"data\":{\"base\":\"BTC\",\"currency\":\"USD\",\"amount\":\"$d\"}}"
|
val json = "{\"data\":{\"base\":\"BTC\",\"currency\":\"USD\",\"amount\":\"$d\"}}"
|
||||||
val price = json.toPrice()
|
val price = json.toPrice()
|
||||||
assertEquals("BTC", price.base, "base is BTC")
|
assertEquals("BTC", price.base, "base is BTC")
|
||||||
assertEquals("USD", price.currency, "currency is USD")
|
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(
|
assertFailsWith(
|
||||||
message = "double conversion did not fail",
|
message = "double conversion did not fail",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue