Solved

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

Posted on 2006-11-17
10
256 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

746 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

9 Experts available now in Live!

Get 1:1 Help Now