Is there an easy way to read back if the language set uses a decimal comma or a decimal point?
EDIT: Updating based on #Algar's suggestion; you can directly use:
char separatorChar = DecimalFormatSymbols.getInstance().getDecimalSeparator();
As it will always return an instance of DecimalFormatSymbols.
NumberFormat nf = NumberFormat.getInstance();
if (nf instanceof DecimalFormat) {
DecimalFormatSymbols sym = ((DecimalFormat) nf).getDecimalFormatSymbols();
char decSeparator = sym.getDecimalSeparator();
}
Docs:
NumberFormat, DecimalFormat, DecimalFormatSymbols
According to the DecimalFormat docs, apparently calling NumberFormat.getInstance() is safe, but may return a subclass other than DecimalFormat (the other option I see is ChoiceFormat). I believe for the majority of instances it should be a DecimalFormat, and then you can compare decSeparator against a , and . to see which format it is using.
Or why not
DecimalFormatSymbols.getInstance().decimalSeparator
I did try this and it worked fine...
String osVersion = System.getProperty("os.version");
String PhoneModel = android.os.Build.MODEL;
String locale = this.getResources().getConfiguration().locale.getDisplayCountry();
char decSeparator = '*';
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
decSeparator = dfs.getDecimalSeparator();
String androidVersion = android.os.Build.VERSION.RELEASE;
String prologue = String.format("OS verson = %s PhoneModel = %s locale = %s DecimalFormatSymbol = [%c] androidVersion = %s ",
osVersion ,PhoneModel, locale, decSeparator,androidVersion);
you can use:
Currency currency = Currency.getInstance(device_locale);
than use currency.getSymbol() for symbol. For default device locale you can use:
#TargetApi(Build.VERSION_CODES.N)
public static Locale getCurrentLocale(Context c) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return c.getResources().getConfiguration().getLocales().get(0);
} else {
//noinspection deprecation
return c.getResources().getConfiguration().locale;
}
}
Not sure if there is an easy way, but you could test which language set is being used and then make the appropriate changes according to if that language used commas or decimals.
Related
I've try this in my device and work fine. But, in some Android device, the symbol is in wrong place. This is my code :
public static String convertToRupiah(String priceBeforeConverted){
//manual setting separator, because currently RUPIAH is NOT supported
DecimalFormat formatter = (DecimalFormat) DecimalFormat.getCurrencyInstance();
DecimalFormatSymbols formatRupiah = new DecimalFormatSymbols();
formatRupiah.setCurrencySymbol("Rp ");
formatRupiah.setMonetaryDecimalSeparator(',');
formatRupiah.setGroupingSeparator('.');
formatter.setDecimalFormatSymbols(formatRupiah);
Double price = StringFormatter.isNullOrEmpty(priceBeforeConverted) ? 0.00 : Double.valueOf(priceBeforeConverted) ;
String conversionResult = formatter.format(price);
if(conversionResult.endsWith(",00"))
conversionResult = conversionResult.substring(0, conversionResult.length()-3);
return conversionResult;
}
expected result is : Rp 25.000,00
String pattern = "Rp ###,###.000 ";
DecimalFormat decimalFormat = new DecimalFormat(pattern);
String format = decimalFormat.format(25.000);
System.out.println(format);
--Try this, your zeros after decimal place will not miss. Output of this code is.
Rp 25.000
Let me know if anything is not clear.
I have a method that converts numeric values to a formatted string. It works well, but only on versions less than android 7. On Android 7 and above it is ignored.
public static String printableSumSeparators(double sum, String format) {
DecimalFormatSymbols dfSymbols = new DecimalFormatSymbols();
dfSymbols.setDecimalSeparator(COMMA_SEPARATOR);
dfSymbols.setGroupingSeparator(' ');
DecimalFormat df = new DecimalFormat(format, dfSymbols);
df.setGroupingUsed(true);
return df.format(sum);
}
Where COMMA_SEPARATOR = ',';
The input of the method is fed with 2 parameters, the number itself for the conversion and the format in the form: ##.0#
Example:
Input: 500000
Output: 500 000
But on the android 7 it does not work and the number is not formatted
Question: How to fix this bug and make it work on the latest version of android
On the latest versions of android you also need to setGroupingSize. Like this:
DecimalFormatSymbols dfSymbols = new DecimalFormatSymbols();
dfSymbols.setDecimalSeparator(',');
dfSymbols.setGroupingSeparator(' ');
DecimalFormat df = new DecimalFormat(format, dfSymbols);
df.setGroupingSize(3);
df.setGroupingUsed(true);
Maybe below code is useful for you, because its work like separating the decimal value using comma's.
protected String getCommaSeparated(String s)
{
try
{
if (s.length() > 0)
{
s =
customFormat("##,##,##0",Double.parseDouble(s.toString().replace(",","")));
if(s.length() > 0)
{
return s;
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return s;
}
public String customFormat(String pattern, double value)
{
DecimalFormat myFormatter = new DecimalFormat(pattern);
String output = myFormatter.format(value);
return output;
}
I'm getting a string from Json response as follows:
"Your account balance is 100000 as on Monday"
In this I need to add comma to the numerical value. Can anyone please help me how I can add this.Thanks in advance.
In this case, I would like to have the output in the following format:
"Your account balance is 100,000 as on Monday"
NumberFormat anotherFormat = NumberFormat.getNumberInstance(Locale.US);
if (anotherFormat instanceof DecimalFormat) {
DecimalFormat anotherDFormat = (DecimalFormat) anotherFormat;
anotherDFormat.applyPattern("#.00");
anotherDFormat.setGroupingUsed(true);
anotherDFormat.setGroupingSize(3);
for (double d : numbers) {
System.out.println(anotherDFormat.format(d));
}
}
final String jsonString = "Your account balance is 100000 as on Monday";
DecimalFormat decFormat = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
decFormat.setGroupingUsed(true);
decFormat.setGroupingSize(3);
Pattern p = Pattern.compile("-?\\d+");
Matcher m = p.matcher(jsonString);
while (m.find()) {
System.out.println(decFormat.format(Double.valueOf(m.group())));
}
I am Android developer.I get from web service result 45.0000.i am stored ArrayList.But i want show it point after two digit only.
Example output 45.00 only .Please give me solution.
Use DecimalFormat class to achieve this.
DecimalFormat df=new DecimalFormat("##.##");
String formate = df.format(value);
double finalValue = (Double)df.parse(formate) ;
you can use String.format, it returns a formatted string using the specified format string and arguments
String.format("$%.2f", value);
also String.format uses your Locale
int index = 0;
for (; index < yourList.size (); index++) {
String tmpvalue = yourList.get(index);
String conValue = String.format("$%.2f", Float.parse(tmpvalue));
yourList.set(index, conValue);
index++;
}
Is there an easy formatter to format my String as a price?
So my string is: 300000 i'd like to "300 000" with space
or 1000000 "1 000 000"
Leslie
This does it:
String s = (String.format("%,d", 1000000)).replace(',', ' ');
Use Formatter class to format string
format("%,d", 1024);
After that replace , with space.
http://developer.android.com/reference/java/util/Formatter.html
You cannot do this with a simple format string but with the DecimalFormat and DecimalFormatSymbols class.
int value = 123456789;
DecimalFormat fmt = new DecimalFormat();
DecimalFormatSymbols fmts = new DecimalFormatSymbols();
fmts.setGroupingSeparator(' ');
fmt.setGroupingSize(3);
fmt.setGroupingUsed(true);
fmt.setDecimalFormatSymbols(fmts);
TextView txt = (TextView) findViewById(R.id.txt);
txt.setText(fmt.format(value));
There are lots and lots of other options in these classes. For example you could seperate the numbers with dots or commas or use a locale specific setting.
For example you can use the fmt.setCurrency method:
fmt.setCurrency(Currency.getInstance(Locale.GERMANY));
double yourPrice = 1999999.99;
String formattedPrice = new DecimalFormat("##,##0.00€").format(yourPrice);
output :
formattedPrice = 1.999.99,99€
if you have integer value, most elegant in my opinion
public static String formatNumberWithSpaces(long number) {
DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance();
DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();
symbols.setGroupingSeparator(' ');
return formatter.format(number);
}