java - How to parse an integer, when I have a format for a double? -
i'm taking user entries database, stores values user entered might either float or int, code meant check following condition
string pattern = "###,###.##"; decimalformat decimalformat = new decimalformat(pattern); if (!userput.equals("")){ resultoutput.settext(""+double.parsedouble(userput)*double.parsedouble(decimalformat.format(double.parsedouble(x[1]))));} else{ userinput.settext("1"); resultoutput.settext(""+double.parsedouble(decimalformat.format(double.parsedouble(x[1])))); }
so when encounters int
crashes. example, 13152 gives java.lang.numberformatexception: invalid double: "13,152"
moreover if output 13170.00
error follows java.lang.numberformatexception: invalid double: "13,170.00"
sometimes values fetched database contains float , integer, here x[1]
currency exchange rate , userinput
contains integer
or float
....lets trying usd idr
currency
13170.00
not double
neither int
because error java.lang.numberformatexception: invalid double: "13,170.00"
your problem not in whether number integer or double. problem lies in fact conversion rate has more 3 digits.
this code displaying value (reformatted):
resultoutput.settext( "" + double.parsedouble(userput) * double.parsedouble(decimalformat.format(double.parsedouble(x[1]))));
so, are:
- taking exchange rate, string, , converting double.
- taking resulting double, , converting string, using decimal format.
- taking formatted text, , converting again double
- multiplying double value of user input
- converting result string (without formatting).
the problem lies in step 2 , 3. unnecessary. numbers work.
your format ###,###.##
. let's see how numbers when formatted 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 have conversion rates smaller 4 digits left of decimal point, decimalformat.format()
call converts them string still legal java double. when call double.parsedouble
on string in step 3, good.
but when have large number, such "13152.00"
, is:
- convert double:
13152.0
- convert string format:
13,152.0
- convert double: - doesn't work. java not accepting
,
in inputdouble.parsedouble()
.
so really, conversion should be:
resultoutput.settext( "" + double.parsedouble(userput) * double.parsedouble(x[1]));
this give proper, unformatted number in resultoutput
, without throwing exception.
i'm pretty sure intended decimalformat
in order display number, not in order convert again , again. means should use on result of conversion - instead of doing "" + ...
, gives default formatting.
resultoutput.settext( decimalformat.format( double.parsedouble(userput) * double.parsedouble(x[1])));
be warned, though, not see 2 digits after decimal point format - display 10.0
10
. if want display 2 digits, have use ###,##0.00
format.
Comments
Post a Comment