Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Beginning Java 2
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 26th, 2003, 04:09 PM
Registered User
 
Join Date: Nov 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with Chapter 2, Exercise 2

Hi,

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.

Regards,
Paul.
 
Old November 26th, 2003, 07:09 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Martyn
 
Old November 29th, 2003, 02:22 PM
Authorized User
 
Join Date: Sep 2003
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

> 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)



 
Old December 1st, 2003, 06:13 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Cheers

Martyn





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





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