B4J Question International Currency Symbol and Format

Discussion in 'B4J Questions' started by tdocs2, Sep 2, 2015.

  1. tdocs2

    tdocs2 Well-Known Member Licensed User

    Greetings.

    I do not want to be banned from the forum for being persistent...

    In this Oracle site, there is a description of how to format currencies including number of decimals and symbols. Of course, because JAVA is foreign to me, I have no idea of the scope to be able to provide this functionality in B4J.

    Since I never cease to be amazed at the prowess of some of the senior members and of Erel, I thought I would ask the question. Can the international currency formatting be achieved in B4J with reasonable effort?

    Thank you for your patience and replies.

    Sandy
     
  2. Daestrum

    Daestrum Well-Known Member Licensed User

    Simple example of how to incorporate code like that in your program.
    Code:
    Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
    Log(inline.RunMethod("displayCurrency",Array(45.00)))
    End Sub

    Sub inline As JavaObject
    Return Me
    End Sub

    #if java
    import java.util.*;
    import java.text.NumberFormat;

    static public String displayCurrency(Double value) {
           Locale currentLocale = Locale.getDefault();
           Currency currentCurrency = Currency.getInstance(currentLocale);  
           NumberFormat currencyFormatter =
           NumberFormat.getCurrencyInstance(currentLocale);
           return currencyFormatter.format(value);
    }
    #end if
     
    tdocs2 likes this.
  3. tdocs2

    tdocs2 Well-Known Member Licensed User

    Thank you, Daestrum.

    I know I am imposing on your generosity...

    How could I change the locale to various countries to test that the proper currency symbol and decimal places are diaplayed?

    Best wishes.

    Sandy
     
    Last edited: Sep 3, 2015
  4. Daestrum

    Daestrum Well-Known Member Licensed User

    Slight change to the coding
    Code:
    ...
    Log(inline.RunMethod("displayCurrency",Array(45.00,"en","US")))
    ...

    #if java
    import java.util.*;
    import java.text.NumberFormat;

    static public String displayCurrency(Double value,String lang, String region) {
       Locale currentLocale = new Locale.Builder().setLanguage(lang).setRegion(region).build();
       Currency currentCurrency = Currency.getInstance(currentLocale);
       NumberFormat currencyFormatter =
       NumberFormat.getCurrencyInstance(currentLocale);
       System.out.println("Language : ["+ currentLocale.getLanguage() +"] Country : [" +  currentLocale.getCountry() + "]");

    return currencyFormatter.format(value);
    }
    #end if
    For easier to read Country and Language change the System.out... line to
    Code:
    System.out.println("Language : ["+ currentLocale.getDisplayLanguage() +"] Country : [" + currentLocale.getDisplayCountry() + "]");
     
    Last edited: Sep 3, 2015
    tdocs2 likes this.
  5. tdocs2

    tdocs2 Well-Known Member Licensed User

    Thank you so much, Daestrum.

    Obviously, your knowledge of JAVA is superior. You have been very kind and generous. I will be able to test it later on today.

    The coding you shared assumes that the number of digits in the currency is known (45.00). There are no currency types in SQLite and I am storing the amount as an Int or 4500... How do I determine the number of decimals required in the currency format? Some countries would have no decimals in their currency while the majority have 2 digits.

    I am very grateful for your help.

    Sandy
     
  6. Daestrum

    Daestrum Well-Known Member Licensed User

    Just use Double to store the value with or without decimal places. The routine seems able to ignore the decimal places in currency that have none.
     
  7. Enrique Gonzalez R

    Enrique Gonzalez R Well-Known Member Licensed User

    i found the solution i post it here for anyone who wants to know!

    Code:
    #if java
    static public Integer displayfractions() {
    Currency curr1 = Currency.getInstance(Locale.getDefault());
    int Digits = curr1.getDefaultFractionDigits();
        return Digits;
    }
    #end if
    use it like the daestrum example
     
    tdocs2 likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice