B4J Question International Currency Symbol and Format

tdocs2

Well-Known Member
Licensed User
Longtime 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
 

Daestrum

Expert
Licensed User
Longtime User
Simple example of how to incorporate code like that in your program.
B4X:
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
 
Upvote 0

tdocs2

Well-Known Member
Licensed User
Longtime 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:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Slight change to the coding
B4X:
...
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
B4X:
System.out.println("Language : ["+ currentLocale.getDisplayLanguage() +"] Country : [" + currentLocale.getDisplayCountry() + "]");
 
Last edited:
Upvote 0

tdocs2

Well-Known Member
Licensed User
Longtime 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
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime 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.
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
i found the solution i post it here for anyone who wants to know!

B4X:
#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
 
Upvote 0
Top