Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Casting doubles to integer problem

Posted on 2003-11-23
5
Medium Priority
?
487 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:antt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9806260
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);
0
 
LVL 15

Expert Comment

by:JakobA
ID: 9806630
double d = Double.parseDouble("19.99");
int cents = Math.round( d * 100 );
0
 
LVL 15

Accepted Solution

by:
JakobA earned 100 total points
ID: 9806636
Oops. it needs an explicit cast:
int cents = (int)Math.round( d * 100 );
0
 

Author Comment

by:antt
ID: 9809882
dammit still doesnt work
0
 

Author Comment

by:antt
ID: 9809896
oh wait yes it does

i cant count
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question