Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Beginning Java 2
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Java 2
This is the forum to discuss the Wrox book Beginning Java 2, SDK 1.4 Edition by Ivor Horton; ISBN: 9780764543654
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Java 2 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 26th, 2003, 04:09 PM
Registered User
Join Date: Nov 2003
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with Chapter 2, Exercise 2


I would be very grateful if someone could shed some light on this simple problem.

Basically I am trying to do Exercise 2 in Chapter 2 of Ivor Horton's Begining Java 2 SDK 1.4 Edition.

The exercise asks that you take a double like 1234.5678. Put the integral part of the double into a long i.e. 1234. This part is OK. Then the exercise asks that you put the fraction into a short i.e. 5678.

This last part is where I am having problems. Other than hard-coding a fix is there a neater way of doing this. Is there a function available already that does this trick?

I am only begining with Java so any help at all would be gratefully received.

  #2 (permalink)  
Old November 26th, 2003, 07:09 PM
Friend of Wrox
Join Date: Jun 2003
Location: , , .
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts

Here's Ivor's solution:

public class DisplayParts
  public static void main(String[] args)
    // Initialize a double variable to some value (1234.5678)
    double myNumber = 1234.5678;
    long integPart = 0;
    short fracPart = 0;
    int decPlaces = 4;

    // Find the integer part
    integPart = (int)myNumber;
    // Find the number of Decimal places
    fracPart = (short)Math.round((myNumber - integPart)*Math.pow(10,decPlaces));

    System.out.println("myNumber is " + integPart + "." + fracPart);

    // What happens if decPlaces > 4? How could this be rectified?

I hope this helps

  #3 (permalink)  
Old November 29th, 2003, 02:22 PM
Authorized User
Join Date: Sep 2003
Location: den Haag, , Netherlands.
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts

> int decPlaces = 4;
> fracPart = (short)Math.round((myNumber - integPart)*Math.pow(10,decPlaces));

Fancy solution, Ivor, congratulations!

My Question:

In the BigDecimal class we have a variable called "scale", that sets the number of digits.
Is there a similar field or method available in the encapsulating Double class?
(Excuse me my jargon practise)

  #4 (permalink)  
Old December 1st, 2003, 06:13 AM
Friend of Wrox
Join Date: Jun 2003
Location: , , .
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts

No, there isn't a scale() method in Double.



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in the exercise chapter 6 derrida BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 March 16th, 2007 08:48 AM
Chapter 5 - Exercise 1 scgtman BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 3 May 16th, 2006 08:10 PM
Chapter 3 Exercise 3 Matt WAXON BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 3 July 4th, 2005 02:19 AM
Chapter 2 Last Exercise has some problem james_027 BOOK: Beginning JavaServer Pages 1 May 25th, 2005 08:19 PM
Problem with exercise in chapter 19 backslash BOOK: Beginning Visual C++ 6 4 September 5th, 2003 11:46 AM

All times are GMT -4. The time now is 07:43 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.