DecimalFormat using Turkish Format - android

My format is
DecimalFormat decimalformat = new DecimalFormat("0.00##");
But my user is Turkish,Russian etc. They different local. They using my app . Give error look like this
For Turkish ,"10,00"
My Error;
java.lang.NumberFormatException:Invalid Double:"10,00"

Have not enough information how do you parse the input, but i suggest you to do it this way:
static String parseInput(String input) throws ParseException {
DecimalFormat dFormat = new DecimalFormat("0.00##");
Number number = dFormat.parse(input);
// double numAsDouble = number.doubleValue();
return number.toString();
}
Not really nice, but you could also try to parse the input by replacing ',':
static String parseInputByReplace(String input) throws ParseException {
DecimalFormat dFormat = new DecimalFormat("0.00##");
double number = Double.parseDouble(input.replace(',', '.'));
return dFormat.format(number);
}

You should parse using the culture of the user. decimal.Parse will do that. If you use a culture of your own it means you are explicitly "telling" the parser to only use that format.

Related

Android - Decimal Formatter

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.

How to keep the Double(1000) value to be in required currency format?

I need to pass a fare amount to the payment gateway. It was working good in English language setting. While I changed the language into Burmese it changes like this 000000၁၀၀၀၀၀, so I am getting invalid amount.Even I have set currency as Locale.English.But didnt work it.
Your answer is more appreciated!!!
try {
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
return nf.parse(s).doubleValue();
} catch (java.text.ParseException e) {
e.printStackTrace();
return 0.0;
}
You can do like this,
//calling of method
String your_amount= setCurrencyFormat(your_string);
private String setCurrencyFormat(String amt) {
if (amt.equals("null") || amt.equals("")) {
amt = "0";
}
double amount = Double.parseDouble(amt);
//here i have done it for Indian currency i.e "in" you can opt for other countries currency
Format formatter = NumberFormat.getCurrencyInstance(new Locale("en", "in"));
return formatter.format(amount);
}
Note: I get my amount in string format you can covert you amt to string if it is in integer format

android to add comma for the numbers which are in String

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())));
}

How to show it point after two digit in java?

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++;
}

Android String format - price

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);
}

Categories

Resources