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.
Related
I'm having trouble formatting this decimal value format (8487.0) for that format. "8.48".
I have tried the solution of other issues posted here but I did not succeed.
Example:
Double n1 = Double.parseDouble (String.valueOf (8487.0));
DecimalFormat dec = new DecimalFormat ("#. ##");
String credits = dec.format (n1); Log.d (TAG, "test" + credits);
Currently it has the output like this: 8487
Any help is welcomed, Thanks.
You can use String format
String answer = String.format("%.2f", (8487.0 / 1000));
Log.d(TAG, answer); //8.49
There are two cases; whether you want to round off or not. Here's the sample code for both the cases.
double d = 8487.0;
d /= 1000;
DecimalFormat f = new DecimalFormat("##.00");
f.setRoundingMode(RoundingMode.FLOOR);
String notRounded = f.format(d);
System.out.println("Not Rounded: " + notRounded);
f.setRoundingMode(RoundingMode.HALF_UP);
String rounded = f.format(d);
System.out.println("Rounded: " + rounded);
So, do you want to reduce the number by a factor 1000 ? e.g. g to kg ?
Double n1 = Double.parseDouble (String.valueOf (8487.0));
Double n2 = n1 / 1000;
DecimalFormat dec = new DecimalFormat ("#. ##");
String credits = dec.format(n2);
Log.d (TAG, "test" + credits);
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())));
}
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.
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);
}