Added toCurrency
This commit is contained in:
parent
94193e34ed
commit
2590fe67b8
4 changed files with 33 additions and 1 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue