Solved

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

Posted on 2006-11-17
10
259 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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 …

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now