# Casting doubles to integer problem

Hiya

I have a small problem when trying to cast a double precision number to an integer:

When I input a double number into my program (its a command line program) I want to immediatly multiply it by 100 and convert it to an integer.  The user should always be inputing numbers with no more than 2 decimal places as it will always be an amount of money.

Basically it usually works for most numbers, but certain numbers such as 19.99 gets converted to 1998 (or 1998.99999999999 when I output the multipled double).

I was just wondering if there was a simple fix for this.
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
The manipulation of floating point values is constrained by degrees of precision - they frequently therefore end up as approximations. For arbitrary precision, use the BigDecimal class:

String s = "19.99";
double d = Double.parseDouble(s);
System.out.println(s + " (parseDouble) -> " + d * 100);
BigDecimal bd = new BigDecimal(s);
d = bd.doubleValue();
System.out.println(s + " (BigDecimal) -> " + d);
Commented:
double d = Double.parseDouble("19.99");
int cents = Math.round( d * 100 );
Commented:
Oops. it needs an explicit cast:
int cents = (int)Math.round( d * 100 );

Experts Exchange Solution brought to you by