Your problem is not in whether the number is integer or double. Your problem lies in the fact that your conversion rate has more than three digits.
This is your code for displaying the value (reformatted):
resultoutput.setText(
""
+ Double.parseDouble(userput)
* Double.parseDouble(decimalFormat.format(Double.parseDouble(x[1]))));
So, you are:
- Taking your exchange rate, which is a string, and converting it to double.
- Taking the resulting double, and converting it back to string, using a decimal format.
- Taking that formatted text, and converting it again to double
- Multiplying it by the double value of the user input
- Converting the result to a string (without formatting).
The problem lies in step 2 and 3. They are actually unnecessary. But for some numbers they will work.
Your format is ###,###.##
. Let's see how some numbers look when they are formatted with this format:
╔═════════╤═══════════╗
║ number │ formatted ║
╠═════════╪═══════════╣
║ 0.273 │ 0.27 ║
╟─────────┼───────────╢
║ 5.3 │ 5.3 ║
╟─────────┼───────────╢
║ 358.2 │ 358.2 ║
╟─────────┼───────────╢
║ 10.0 │ 10 ║
╟─────────┼───────────╢
║ 1298.52 │ 1,298.52 ║
╚═════════╧═══════════╝
So, when you have conversion rates that are smaller than four digits to the left of the decimal point, the decimalFormat.format()
call converts them to a string that is still a legal Java double. So when you then call Double.parseDouble
on this string in step 3, everything is good.
But when you have a large number, such as "13152.00"
, what you do is:
- Convert it into double:
13152.0
- Convert it into string with a format:
13,152.0
- Convert it into double: - this doesn't work. Java is not accepting
,
in the input for Double.parseDouble()
.
So really, your conversion should just be:
resultoutput.setText(
""
+ Double.parseDouble(userput)
* Double.parseDouble(x[1]));
This will give you a proper, unformatted number in your resultoutput
, without throwing your an exception.
I'm pretty sure you intended the decimalFormat
in order to display the number, not in order to convert it again and again. This means you should only use it on the result of the conversion - instead of doing "" + ...
, which gives you just default formatting.
resultoutput.setText(
decimalFormat.format( Double.parseDouble(userput) * Double.parseDouble(x[1])));
Be warned, though, that you will not see two digits after the decimal point with your format - it will display 10.0
as 10
. If you want to always display two digits, you have to use ###,##0.00
as your format.