Trying to figure out why the following code is not outputting expecting results. Please advise. Thank you.
import java.text.*;
public class Test {
public static void main(String[] args) {
String s = "987.123456";
double d = 987.123456d;
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(5);
System.out.println(nf.format(d) + " ");
try {
System.out.println(nf.parse(s));
} catch (Exception e) {
System.out.println("got exc");
}
}
}
Output:
987.12346 // Expected 987.12345 not 987.12346
987.123456
Your second print doesn't format the double you've parsed.
// System.out.println(nf.parse(s));
System.out.println(nf.format(nf.parse(s))); // <-- 987.12346
To get the output you asked for, you can add a call to NumberFormat#setRoundingMode(RoundingMode) - something like
nf.setMaximumFractionDigits(5);
nf.setRoundingMode(RoundingMode.DOWN);
Related
I am attempting to use MessageFormat class to parse a message. But I get "MessageFormat parse error!". I got this code from internet. Here is the link:
package myy.test;
import java.text.MessageFormat;
import java.text.ParseException;
public class TestParse {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// creating and initializing MessageFormat
MessageFormat mf
= new MessageFormat("{0, number, #}, {2, number, #.#}, {1, number, #.##}");
;
// creating and initializing String source
String str = "10.456, 20.325, 30.444";
System.out.println(str);
// parsing the string
// accoridng to MessageFormat
// using parse() method
Object[] hash = mf.parse(str);
// display the result
System.out.println("Parsed value are :");
for (int i = 0; i < hash.length; i++)
System.out.println(hash[i]);
}
catch (ParseException e) {
System.out.println("\nString is Null");
System.out.println("Exception thrown : " + e);
}
}
}
I get the following output in the console.
10.456, 20.325, 30.444
String is Null
Exception thrown : java.text.ParseException: MessageFormat parse error!
Why do I get this error and how do I resolve it? Thanks.
I changed the parameters in the constructor to this
MessageFormat mf = new MessageFormat("{0,number,#,###.##}, {2,number,#,###.##}, {1,number,#,###.##}");
Consolo output is like this:
10.456, 20.325, 30.444
Parsed value are :
10.456
30.444
20.325
As always when encountering parsing issues, try doing the reverse operation to see when input the parse is expecting. This is a general guideline that applies to XML, JSON, Dates, and to MessageFormat.
MessageFormat mf
= new MessageFormat("{0, number, #}, {2, number, #.#}, {1, number, #.##}");
;
System.out.println(mf.format(new Integer[] { 10456, 30444, 20325 }));
Output
10456, 20325, 30444
As you can see, the output has leading spaces. If we change to:
String str = " 10.456, 20.325, 30.444";
Then it all works.
Output
10.456, 20.325, 30.444
Parsed value are :
10.456
30.444
20.325
I'm using NumberFormat in my app to get the currency formatted Strings. Like if a user inputs 12.25 in the field then it will be changed to $12.25 based on locale. Here the Locale is en-US.
Now I want to get the 12.25 value as double form the formatted string.
To do that I have used:
NumberFormat.getCurrencyInstance().parse("$12.25").doubleValue();
Above line giving me the result of 12.25 which is my requirement. But suppose a user changed his locale to something else en-UK. Now for that locale above statement is giving me parseException. Because for the locale en-UK, currency string $12.25 is not parsable.
So is there any way to get the double value from currency formatted string irrespective of what the locale is?
I don't know either the below solution is perfect or not but it is working according to my requirement.
try {
return NumberFormat.getCurrencyInstance().parse(currency).doubleValue();
} catch (ParseException e) {
e.printStackTrace();
// Currency string is not parsable
// might be different locale
String cleanString = currency.replaceAll("\\D", "");
try {
double money = Double.parseDouble(cleanString);
return money / 100;
} catch (Exception ex) {
ex.printStackTrace();
}
}
return 0;
What about
new Double(NumberFormat.getCurrencyInstance().parse("$12.25").doubleValue());
and also you could use
Double.valueOf() creates a Double object so .doubleValue() should not be necessary.
also Double.parseDouble(NumberFormat.getCurrencyInstance().parse("$12.25"));
could work
Here's a little algorithm that may help you :
public static void main(String[] args) {
String cash = "R$1,000.75"; //The loop below will work for ANY currency as long as it does not start with a digit
boolean continueLoop = true;
char[] cashArray = cash.toCharArray();
int cpt = 0;
while(continueLoop){
try
{
double d = Double.parseDouble(cashArray[cpt]+"");
continueLoop = false;
}catch(NumberFormatException nfe){
cpt += 1;
}
}
System.out.println(cpt);
//From here you can do whatever you want....
String extracted = cash.substring(cpt);
NumberFormat format = NumberFormat.getInstance(Locale.US); //YOUR REQUIREMENTS !!!! lol
try {
Number youValue = format.parse(extracted);
System.out.println(youValue.doubleValue());
} catch (ParseException ex) {
//handle your parse error here
}
}
You should get as result here in the output:
2
1000.75
I am working on an Android app, and I am getting the most annoying NFE for, what seems like, no reason.
So, here is what I have in my app:
int amount = 7;
NumberFormat myNumberFormat = NumberFormat.getCurrencyInstance(Locale.US);
TextView money = (TextView)findViewById(R.id.money_view);
money.setText(myNumberFormat.format(amount));
And for some reason, I am getting a NFE when I try to get the NumberFormat currency instance. As a test, to make sure I wasn't going crazy, I also wrote this stand-alone:
import java.util.Locale;
import java.text.NumberFormat;
public class NFETest {
public static void main(String[] args){
int amount = 7;
NumberFormat myNumberFormat = NumberFormat.getCurrencyInstance(Locale.US);
System.out.println(myNumberFormat.format(amount));
}
}
The stand-alone works with no problems. So, what gives ... why am I getting this error?
EDIT:
Looking further down LogCat, it looks like it is an IllegalArgumentException instead of a NFE. However, this doesn't make it any less strange. I have "Locale.US" set, so that shouldn't make any difference. However, some quick googling says it may be my tablet thinking it is not in the US. It may be a hardware issue, and not software after all.
Use this code
int amount = 7;
NumberFormat myNumberFormat = NumberFormat.getCurrencyInstance(Locale.US);
TextView money = (TextView)findViewById(R.id.money_view);
money.setText(String.valueOf(myNumberFormat.format(amount)));
Just use wrappers(Integer, Float, Double, BigDecimal or others):
public static void main(String[] args){
Integer amount = 7;
NumberFormat myNumberFormat = NumberFormat.getCurrencyInstance(Locale.US);
System.out.println(myNumberFormat.format(amount));
int amount2 = 11;
System.out.println(myNumberFormat.format(Integer.valueOf(amount2)));
}
In other solution, you may use Integer.valueOf(..) Look like here: http://ideone.com/0ZKsS3
But your example works too. Look at online compiler: http://ideone.com/0ZKsS3
try {
int amount = 7;
NumberFormat myNumberFormat = NumberFormat.getCurrencyInstance(Locale.US);
TextView money = (TextView)findViewById(R.id.money_view);
money.setText(String.valueOf(myNumberFormat.format(amount)));// your error is here.
//go on as normal
} catch (NumberFormatException e) {
//handle error
}
you should catch the exception and handle the parse error accordingly.
Or you should try for different values :
double num = 1323.526;
NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
System.out.println("US: " + defaultFormat.format(num));
Locale swedish = new Locale("sv", "SE");
NumberFormat swedishFormat = NumberFormat.getCurrencyInstance(swedish);
System.out.println("Swedish: " + swedishFormat.format(num));
OUTPUT :
US: $1,323.53
Swedish: 1 323,53 kr
Hope this time it will help you to catch your problem.
i try to save my String value (50000000) into Double format, while I'm trying to show it again in my Edittext, I can't to show it in normal format, and it show as (5E+07), is there any way to convert from double format into String format?
I have try this way :
Double value_doble = 5E+07;
EditText.setText(String.valueOf(value_doble);
but its Still show as 5E+07, so my question how to convert from Double to String?
You can try this:
System.out.println(new BigDecimal(value_doble).toString());
Is this what you are looking for?
public static void main(String[] args) {
Double value_doble = 5E+07;
NumberFormat formatter = new DecimalFormat("###.#####");
String f = formatter.format(value_doble);
System.out.println(f);
}
I agree that you need use Formater
http://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html
but the pattern should be look like this:
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class DoubleFormat {
public static void main(String[] args) {
double valueD = 5E+07;
NumberFormat format = new DecimalFormat("#");
System.out.println(format.format(valueD));
}
}
I'm making an Android Java program which is taking double values from the user. If I run the program on the computer, it works great because of the locale of my computer, EN_UK. But when I run it on my mobile phone with FI_FI locale, it won't work. I know the reason: In UK, people use dot as decimal separator but here in Finland, the decimal separator is comma.
DecimalFormat df = new DecimalFormat("#.#");
Double returnValue = Double.valueOf(df.format(doubleNumber));
When I'm using comma, it says java.lang.NumberFormatException: Invalid double: "1234,5".
How can I make it work with them both, comma and dot?
Use one of the other constructors of DecimalFormat:
new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.US))
And then try and parse it using both separators.
using DecimalFormatSymbols.getInstance() will produce the default locale's correct symbols, so you will get it right for any platform you run on.
DecimalFormat df = new DecimalFormat("#.#", DecimalFormatSymbols.getInstance());
This should work for both Java(Tested) as well as android :)
Class Name: In18Helper.java
package com.akmeher.app.utils;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
public class In18Helper {
private final static In18Helper mHelper = new In18Helper();
public static final In18Helper getInstance() {
return mHelper;
}
public double getDouble(String sValue, Locale locale) {
NumberFormat numberFormat = NumberFormat.getInstance(locale);
Number parse = null;
try {
parse = numberFormat.parse(sValue);
} catch (ParseException e) {
e.printStackTrace();
}
return parse == null ? 0 : parse.doubleValue();
}
}
Class Name: Application.java
package com.akmeher.app;
import java.util.Locale;
import com.akmeher.app.utils.In18Helper;
public class Application {
static DataModel[] testData = new DataModel[] {
new DataModel("1.034567", Locale.ENGLISH),
new DataModel("1,0345.67", Locale.ENGLISH),
new DataModel("1.0345,67", Locale.GERMANY),
new DataModel("1,034,567", Locale.CANADA),
new DataModel("1.034567", Locale.KOREA),
new DataModel("1,03.4567", Locale.ITALY) };
/**
* #param args
*/
public static void main(String[] args) {
for (int i = 0; i < testData.length; i++) {
double d = In18Helper.getInstance().getDouble(testData[i].mValue,
testData[i].mLocale);
System.out.println("Trial Value: "+testData[i].mValue+" for Locale: "+testData[i].mLocale+" converted to: "+d);
}
}
private static class DataModel {
String mValue;
Locale mLocale;
public DataModel(String value, Locale locale) {
this.mLocale = locale;
this.mValue = value;
}
}
}
Output:
Trial Value: 1.034567 for Locale: en converted to: 1.034567
Trial Value: 1,0345.67 for Locale: en converted to: 10345.67
Trial Value: 1.0345,67 for Locale: de_DE converted to: 10345.67
Trial Value: 1,034,567 for Locale: en_CA converted to: 1034567.0
Trial Value: 1.034567 for Locale: ko_KR converted to: 1.034567
Trial Value: 1,03.4567 for Locale: it_IT converted to: 1.03
Hope this will help somebody to make use of.
public static Double parseDoubleTL(String value){
DecimalFormat df = new DecimalFormat("#.#", new DecimalFormatSymbols(new Locale("tr_TR")));
Double doublePrice = 0.0;
try {
doublePrice = df.parse(value).doubleValue();
} catch (ParseException e) {
Log.w(MainActivity.TAG,"Couldnt parse TL. Error is "+e.toString());
}
return doublePrice;
}
Not a best way but worked for me;
Double val=null;
try{
val=Double.valueOf(value);
}catch(Exception e){
val=Double.valueOf(value.replace(',','.'));
}
Double val=null;
try{
val=Double.valueOf(value);
}catch(Exception e){
val=Double.valueOf(value.replace(',','.'));
}
return val;
Me Error:
java.lang.NumberFormatException: Invalid float: "1,683.88"
... and this work for me
replace(",", "")
DecimanFormat df = new DecimalFormat("#.#");