Solved

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

Posted on 2006-11-17
10
264 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 97
groupSumClump challenge 9 116
Meaning of "exceptions()" in "ist.exceptions( ist.exceptions() | ios_base:: badbit);" 3 68
SQL Insert parts by customer 12 34
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 …

803 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