I have a legacy program (java 1.4) running under Tomcat/Jboss, however, i have copy it to a new server (java 1.7) and it throws the following exception.
java.lang.NumberFormatException: For input string: "0000000000000000009011,00"
sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1250)
java.lang.Double.valueOf(Double.java:504)
java.lang.Double.<init>(Double.java:597)
Since i don't have access to the source, is there any way to fix this? i haven't try with java 1.4 for the moment
You schould look to jvm parameters on old server. Your language parameters will change comma to dot actually so you do not need any code changes.
I think its not due to java version. Replace comma(,) with "" or period(.)
For example conside a string : String str = "0000000000000000009011,00";
Now you can try -
double result = Double.valueOf(str.replace(",", "."));
or
double result = Double.valueOf(str.replace(",", ""));
Rather use NumberFormat for parsing:
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
Number number = format.parse("0000000000000000009011,00");
double d = number.doubleValue();
This way you can select the right locale for the number representation. In the example above I used France - they use a comma for the decimal point.
Related
I have a problem with parsing decimal number in Linux environment. When I parse in Windows, everything's all right.
Below, code snippet
String price;
DecimalFormat FORMATTER = (DecimalFormat)DecimalFormat.getInstance();
double customPrice = FORMATTER.parse(price).doubleValue();
And results
When price='9' Then customPrice='9.0' - it is ok
When price='1,00' Then customPrice='100.0' - it is wrong
When price='25,00' Then customPrice='2500.0' - it is wrong
Can you tell me what the problem is ?
Thanks
Read here here about locales, probably you're using en_US which causes the ',' symbol to separate groups of thousands.
you can use also
DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols(currentLocale);
unusualSymbols.setDecimalSeparator(',');
DecimalFormat weirdFormatter = new DecimalFormat(strange, unusualSymbols);
weirdFormatter.setGroupingSize(4);
to state your own sepeartors
You can change the decimal separator of DecimalFormat. The easiest way by using a NumberFormat with the desired locale, e.g.:
NumberFormat FORMATTER = NumberFormat.getNumberInstance(Locale.GERMAN);
double customPrice = FORMATTER.parse(price).doubleValue();
When I run this line of code: Float.parseFloat("1460000 JPY") I get the error
Exception in thread "main" java.lang.NumberFormatException: For input string: "1460000 JPY"
This string is coming from an API call from a form where this is a text field with no validation. It usually works because people put in just a number, but sometimes you get this issue. How do I get it to return just the initial numbers as a float and disregard the trailing alpha characters?
You can use regex to find if that string contains only digit or not
String apistring = "1460000 JPY";
if(apistring.matches("[0-9]+")){
// do your code
}else{
// throw some error message
}
Stripping char from that will be difficult as you said its a input field and user can enter any text. You can strip it off only if you know that there is a particular pattern
Since DecimalFormat is very lenient about parsing Strings I would recommend that.
You can use it like this:
DecimalFormat df = new DecimalFormat();
try {
float parsedValue = df.parse("1460000 JPY").floatValue();
System.out.println(parsedValue);
} catch (ParseException pe) {
pe.printStackTrace();
// handle exception a bit more
}
This prints:
1460000.0
As you can see the parse method can throw a ParseException if the passed String starts with something else than a number, like:
blub 1460000 JPY
If that won't happen in your app, then you don't have to bother about it.
You can use regex to extract the numbers in input .
s = s.replaceAll("[^0-9]","");
and then parse float from it. Only downside is that it will extract all numbers (It will extract 1245 and 3 both from 1245 JPY 3).
UPDATE: to account for the bug that #Tom brought up:
Float.parseFloat("1.46 JPY".replaceAll("[^0-9.]",""));
1.46
the above is a superior solution. See below for explanation.
As #azurefrog said, stripping out non-numeric characters and then parsing what is left as a Float is the way to go.You can accomplish this using the following code:
Float.parseFloat("1460000 JPY".replaceAll("[^0-9]",""));
1460000.0
This is not very robust though, because for inputs like "1.46" the output becomes
146.0
.replaceAll("[^0-9.]","") fixes this inaccuracy by adding the decimal . character to the exclusion regex like so [^0-9.]
I'm trying to reformat a string in java and remove the final decimal out of an android version number, so 4.2.2 would be formatted to 4.2.
Would I use REGEX to get the desired format for the android version number?
Here's my code for finding the android version number:
String osRelease = Build.VERSION.RELEASE;
http://i.imgur.com/UHRohGU.jpg [Error Log File From Eclipse]
Just use some String manipulation:
String i = "4.2.2";
String s = i.substring(0, Math.min(s.length(), 3));
System.out.println(s);
This will give you the first 3 characters (ie. "4.2") of your input String).
If you have your value as a Double or similar, use String.valueOf(myDouble) to convert to String.
Math.min() trick is from here.
I'm having the strangest thing with a DecimalFormat.
I'm using it in an webapplication.
I setup some test and it keeps on failing with me locally.
A friend of me ran it and he was able to successfully ran the JUnit tests.
The strange part is that on our server the application runs it perfectly without any problems either.
Could it be that Java depends on the system settings like valuta and number settings?
Or could there be another reason?
This is how my piece of code looks like:
public String generateFormatPrice(double price) throws Exception {
DecimalFormat format1 = new DecimalFormat("#,##0.00");
String tmp = format1.format(price).replace(".", "&");
String[] tmps = tmp.split("&");
return tmps[0].replace(',', '.') + "," + tmps[1];
}
Thanks a lot in advance!
This code is indeed locale-specific. If your code depends on being in a locale such as the USA where "." is the decimal separator and "," is the thousands separator, and then you run this code on a server set to, for example, the German locale, it will fail.
Consider using this constructor, which allows you to explicitly specify which symbols you are using.
EDIT: as far as I can tell you are trying to format numbers using "." as the thousands separator and "," as the decimal separator. In other words, the format used in France and Germany. Here's one approach that will achieve this:
public static String generateFormatPrice(double price) {
final NumberFormat format = NumberFormat.getNumberInstance(Locale.GERMANY);
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(2);
return format.format(price);
}
Also, you shouldn't be using a double to hold a monetary value - you are going to encounter some nasty bugs if you do that.
How do I format a Double with String.format to String with a dot between the integer and decimal part?
String s = String.format("%.2f", price);
The above formats only with a comma: ",".
String.format(String, Object ...) is using your JVM's default locale. You can use whatever locale using String.format(Locale, String, Object ...) or java.util.Formatter directly.
String s = String.format(Locale.US, "%.2f", price);
or
String s = new Formatter(Locale.US).format("%.2f", price);
or
// do this at application startup, e.g. in your main() method
Locale.setDefault(Locale.US);
// now you can use String.format(..) as you did before
String s = String.format("%.2f", price);
or
// set locale using system properties at JVM startup
java -Duser.language=en -Duser.region=US ...
Based on this post you can do it like this and it works for me on Android 7.0
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
DecimalFormat df = new DecimalFormat("#,##0.00");
df.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.ITALY));
System.out.println(df.format(yourNumber)); //will output 123.456,78
This way you have dot and comma based on your Locale
Answer edited and fixed thanks to Kevin van Mierlo comment
If it works the same as in PHP and C#, you might need to set your locale somehow. Might find something more about that in the Java Internationalization FAQ.