Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

I need the entire result of a divison, remainder and all

Posted on 2006-11-17
10
Medium Priority
?
279 Views
Last Modified: 2008-03-03
86399 / 3600 = 23.999722222222222222222222222222

I need this whole number, or at least to 10 places or so.

How can I make the entire number show in Java?
0
Comment
Question by:arctanx
[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
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 2

Author Comment

by:arctanx
ID: 17966420
Whoops I meant for this to be in the Java section.

Can someone move for me?
0
 
LVL 24

Expert Comment

by:SunBow
ID: 17966558
Using integer is a good way
If 'i' is integer, then the whole part
i = 86399 / 3600
then the remainder (non-integer) is (86399 / 3600) - i
or remainder is -i + 86399 / 3600
[parens now needed but for clarity]
if i is not integer then use integer function (convert to) on the first division process
0
 
LVL 24

Expert Comment

by:SunBow
ID: 17966579
The number of places is not consequential, other than in defining the types for variables in the first place.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 8

Expert Comment

by:YoungBonzi
ID: 17966586
import java.math.*;

public class Fraction {
    public static double digits(double d, int places) {
       return (long)(d*Math.pow(10,places)+0.5)/Math.pow(10,places);
    }
 
   public static double digits(double d, long places) {
      return (long)(d*Math.pow(10,places)+0.5)/Math.pow(10,places);
   }
 
   public static float digits(float d, int places) {
      return (int)(d*Math.pow(10,places)+0.5)/(float)Math.pow(10,places);
   }
 
   public static float digits(float d, long places) {
      return (int)(d*Math.pow(10,places)+0.5)/(float)Math.pow(10,places);
   }
 }


Usage:

*          double x = 86399 / 3600;
*          System.out.print( Fraction.digits(x,10) ); //  Fraction.digits(double, number of places);
*     // will output the double as : 23.9997222222 (10 placess)
0
 
LVL 8

Expert Comment

by:YoungBonzi
ID: 17966601
Or, for float...
Usage:

*          float y = 86399 / 3600;
*          System.out.print( Fraction.digits(f,10) ); //  Fraction.digits(float, number of places);
*     // will output the float as : 23.9997222222 (10 placess)
0
 
LVL 24

Expert Comment

by:SunBow
ID: 17966647
http://java.sun.com/docs/books/vmspec/html/Overview.doc.html

3.2.1 Integral Types and Values
The values of the integral types of the Java Virtual Machine are the same as those for the integral types of the Java language (§2.4.1):

For byte, from -128 to 127 (-27 to 27-1), inclusive
For short, from -32768 to 32767 (-215 to 215-1), inclusive
For int, from -2147483648 to 2147483647 (-231 to 231-1), inclusive
For long, from -9223372036854775808 to 9223372036854775807 (-263 to 263-1), inclusive
For char, from 'u0000' to 'uffff'; char is unsigned, so 'uffff' represents 65535 when used in expressions, not -1

3.2.2 Floating-Point Types and Values

The largest positive finite floating-point literal of type double is 1.79769313486231570e+308. The smallest positive nonzero floating-point literal of type double is 4.94065645841246544e-324.
0
 
LVL 24

Expert Comment

by:SunBow
ID: 17966683
I misread, thought that the need was to separate out the whole part and the remainder part
0
 
LVL 2

Author Comment

by:arctanx
ID: 17966906
Thanks for the help, but I found my own solution:

double hour = (double)86399 / (double)3600;

Gives me 23.99972222222222, exactly what I need.

BTW that Fraction class didn't work for me, it kept giving me wierd numbers.

thanks all!
0
 

Accepted Solution

by:
PashaMod earned 0 total points
ID: 18012792
Closed, 50 points refunded.
PashaMod
Community Support Moderator
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

715 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