Solved

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

Posted on 2006-11-17
10
270 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
BATCH to EXE Converter 2 99
Assignment from incompatible pointer type? 2 77
Ruby or Python 7 130
Eclipse neon2 "Java build path" correctness 7 43
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 …
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

734 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