javaandroidbilling

How to divide a yearly subscription prince into 12 months in Android?


I have my app in Android that has a yearly subscription. I would like to display to the user the price also in months (so it looks more attractive). For example, let's say the yerly subscription is $12, I want to display someting like this:

Get this subscription for only $1/month (billed as yearly payments of $12)

I looks as simple as dividing the price of the subscription by 12 moths, but I am stragling with the currency symbol. Some locales will display de symbol in the left, others in the right. For example in Spanish it will be "12€".

Is there a way to detect that?

I am having toying with

float fYearPerice = mySkuDetails.getPriceAmountMicros() / 1000000.0f; float fMonthPrice = oSubscripcion.fPrecio/12; String sCurrencyCode = oSkuDetails.getPriceCurrencyCode();

I can do something as simple as this:

String s = "Get this subscription for only " + sCurrencyCode  + fMonthPrice  + "/month (billed as yearly payments of " + mySkuDetails.getPrice();

But the currency will not be in the right position for every locale. Is there a way to approach this elegantly?


Solution

  • Updated as per request to make it more generic:

    import java.text.NumberFormat;
    import java.util.Locale;
    
    public class CurrencyFormatter {
    
        public enum LanguageCode {
            EN, ES, FR // Add more language codes as needed
        }
    
        public enum CountryCode {
            US, ES, FR, GB // Add more country codes as needed
        }
    
        public static void main(String[] args) {
            String stringSpain = getFormattedCurrencyString(LanguageCode.ES, CountryCode.ES, 1f, 12f);
            System.out.println(stringSpain);
    
            String stringFrance = getFormattedCurrencyString(LanguageCode.FR, CountryCode.FR, 1f, 12f);
            System.out.println(stringFrance);
    
            String stringUK = getFormattedCurrencyString(LanguageCode.EN, CountryCode.GB, 1f, 12f);
            System.out.println(stringUK);
    
            String stringUS = getFormattedCurrencyString(LanguageCode.EN, CountryCode.US, 1f, 12f);
            System.out.println(stringUS);
        }
    
        public static String getFormattedCurrencyString(LanguageCode languageCode, CountryCode countryCode, float monthlyPrice, float yearlyPrice) {
            Locale locale = Locale.forLanguageTag(languageCode.name() + "-" + countryCode.name());
            NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(locale);
            String formattedMonthlyPrice = currencyFormat.format(monthlyPrice);
            String formattedYearlyPrice = currencyFormat.format(yearlyPrice);
            return "Get this subscription for only " + formattedMonthlyPrice + "/month (billed as yearly payments of " + formattedYearlyPrice + ")";
        }
    }
    

    This should work fine:

    Locale countryLocaleSpain = Locale.of("es", "ES");
    NumberFormat currencyFormatSpain = NumberFormat.getCurrencyInstance(countryLocaleSpain);
    String stringSpain = "Get this subscription for only " + currencyFormatSpain.format(1f)  + "/month (billed as yearly payments of " + currencyFormatSpain.format(12f);
    System.out.println(stringSpain);
    
    Locale countryLocaleFrance = Locale.FRANCE;
    NumberFormat currencyFormatFrance = NumberFormat.getCurrencyInstance(countryLocaleFrance);
    String stringFrance = "Get this subscription for only " + currencyFormatFrance.format(1f)  + "/month (billed as yearly payments of " + currencyFormatFrance.format(12f);
    System.out.println(stringFrance);
    
    Locale countryLocaleUK = Locale.UK;
    NumberFormat currencyFormatUK = NumberFormat.getCurrencyInstance(countryLocaleUK);
    String stringUK = "Get this subscription for only " + currencyFormatUK.format(1f)  + "/month (billed as yearly payments of " + currencyFormatUK.format(12f);
    System.out.println(stringUK);
    
    Locale countryLocaleUS = Locale.US;
    NumberFormat currencyFormatUS = NumberFormat.getCurrencyInstance(countryLocaleUS);
    String stringUS = "Get this subscription for only " + currencyFormatUS.format(1f)  + "/month (billed as yearly payments of " + currencyFormatUS.format(12f);
    System.out.println(stringUS);
    

    Output:

    Get this subscription for only 1,00 €/month (billed as yearly payments of 12,00 €
    Get this subscription for only 1,00 €/month (billed as yearly payments of 12,00 €
    Get this subscription for only £1.00/month (billed as yearly payments of £12.00
    Get this subscription for only $1.00/month (billed as yearly payments of $12.00