How to get currency symbol from Play Billing Library
Asked Answered
C

4

10

I use Play Billing Library and I have two subscriptions for one and three months. For a 3 month subscription, I want to display the price for 1 month. For example, a 1 month subscription is $1,50 and a 3 month subscription is $3 ($1 for a 1 month). But I have some problems with currencies.

This is a code:

long priceAmountMicros = skuDetails.getPriceAmountMicros(); //3000000
String priceCurrency = skuDetails.getPriceCurrencyCode(); //USD

float priceFloat = (float)priceAmountMicros / 3 / 1000000; //1
String price = new DecimalFormat("#0.00").format(priceFloat);

Currency currency = Currency.getInstance(priceCurrency);
String symbolCurrency = currency.getSymbol();//$

textView.setText(price + " " + symbolCurrency); //1.00 $ or 70.00 руб

The code almost is working. USD converted to $. EUR converted to £ and others. There are some problems with some currencies, for example, the Russian currency does not work. RUB converted to руб, but I expect

There is a standard method for getting the price:

String price = skuDetails.getPrice();//3 $ or 210 ₽

Am I doing something wrong? Could you help me please?

Castor answered 17/7, 2020 at 14:29 Comment(6)
as i can see the library uses ISO 4217 so rouble symbol in ISO 4217 is рубTart
@Tart If I get skuDetails.getPrice(); then it returns the price 210 ₽. But I need to divide this by 3 with currency ₽Castor
I have the same problem. I will checking your question if you found out the problemVanburen
@Castor method currency.getSymbol() from java framework is buggy and does not work PROPERLY - for many currencies it is completely WRONG - for example for THAI baht it returns THB and not currency symbol ฿, as it should. And same for many other currencies, it seems quite random...Jarlath
EUR is € and not £. £ is for GBP.Amative
check this answerTautology
H
8

You can use Currency class from java.util package

Currency.getInstance(sku.priceCurrencyCode).symbol

will give you the symbol for currency code

Horned answered 17/6, 2021 at 13:44 Comment(0)
A
0

One cannot assume that the currency symbol always would be to the left of the price.

Simply strip all numbers and decimal separators:

String symbol = skuDetails.getPrice().replaceAll("[\\d.,]", "");

However, the most easy might be:

textView.setText(skuDetails.getPrice());

I mean, why you even need the currency symbol?

Amaty answered 8/2, 2021 at 23:1 Comment(1)
Because we need modify the price (the number). For example I want to show monthly price for yearly subscription. I need divide price to 12 for that and display formatted result. Will your regex work for Arabic or Chinese numbers??Edythedythe
D
0

getOriginalPrice() - returns the original price with additional currency formatting.

Damoiselle answered 11/2, 2021 at 5:30 Comment(1)
We need to modify the number and present formatted result. For example I need to show monthly price for yearly subscription.Edythedythe
M
0

Use this function to get the monthly price for an annual subscription. Don't use java currency class as it has issues with some currencies.

private fun getAnnualMonthlyPrice(skuDetails: SkuDetails): String {
    val re = Regex("[0-9.,]")
    val currency = re.replace(skuDetails.price, "")
    val priceMicro = skuDetails.priceAmountMicros / (1_000_000f * 12f)
    return "${currency}${"%.2f".format(priceMicro)}"
}
Myeshamyhre answered 20/7, 2021 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.