From 2590fe67b87b0d86fe0def504cf34689520ab820 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Tue, 25 May 2021 17:57:46 -0700 Subject: [PATCH] Added toCurrency --- README.md | 6 ++++++ examples/build.gradle.kts | 2 +- .../kotlin/net/thauvin/erik/crypto/CryptoPrice.kt | 13 +++++++++++++ .../net/thauvin/erik/crypto/CryptoPriceTest.kt | 13 +++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 20ccb22..de1767e 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,12 @@ CryptoPrice(val base: String, val currency: String, val amount: Double) ``` The parameter names match the [Coinbase API](https://developers.coinbase.com/api/v2#get-spot-price). +To display the amount as a fomatted currency use the `toCurrency` function: + +```kotlin +val price = CryptoPrice("BTC", "EUR", 12345.67) +println(price.toCurrency()) // will print €12,345.67 +``` ### Extending A generic `apiCall()` function is available to access other [API data endpoints](https://developers.coinbase.com/api/v2#data-endpoints). For example to retried the current [buy price](https://developers.coinbase.com/api/v2#get-buy-price) of a cryptocurrency: diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts index 1b19674..36e7564 100644 --- a/examples/build.gradle.kts +++ b/examples/build.gradle.kts @@ -1,5 +1,5 @@ plugins { - id("org.jetbrains.kotlin.jvm") version "1.5.0" + id("org.jetbrains.kotlin.jvm") version "1.5.10" id("com.github.ben-manes.versions") version "0.38.0" application } diff --git a/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt b/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt index 884196b..defaed1 100644 --- a/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt +++ b/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt @@ -38,7 +38,9 @@ import okhttp3.Request import org.json.JSONException import org.json.JSONObject import java.io.IOException +import java.text.NumberFormat import java.time.LocalDate +import java.util.Currency /** * A small Kotlin/Java library for retrieving cryptocurrencies current market prices. @@ -134,4 +136,15 @@ open class CryptoPrice(val base: String, val currency: String, val amount: Doubl return body.toPrice() } } + + /** + * Return the [amount] as a currency formatted string. (eg: $1,203.33) + */ + @Throws(IllegalArgumentException::class) + fun toCurrency(): String { + return NumberFormat.getCurrencyInstance().let { + it.setCurrency(Currency.getInstance(currency)) + 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 cd5d7f7..e4608de 100644 --- a/src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt +++ b/src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt @@ -80,6 +80,19 @@ class CryptoPriceTest { } } + @Test + @Throws(IllegalArgumentException::class) + fun testToCurrency() { + val eur = CryptoPrice("BTC", "EUR", 12345.67) + assertEquals(eur.toCurrency(), "€12,345.67", "EUR format") + + val gbp = CryptoPrice("ETH", "GBP", 12345.67) + assertEquals(gbp.toCurrency(), "£12,345.67", "GBP format") + + val aud = CryptoPrice("BTC", "AUD", 12345.67) + assertEquals(aud.toCurrency(), "A$12,345.67", "AUD format") + } + @Test @Throws(CryptoException::class) fun testToPrice() {