Results 1 to 3 of 3
Thread: trouble using fractions
- 09-06-2011, 09:30 PM #1Member
- Join Date
- Sep 2011
- Rep Power
trouble using fractions
I'm new to this forum and to java, and have a rather simple question:
I want to use the fraction 1/12, but as an answere, i always get 0.0.
double a= 1/12;
which will give me the result : a= 0.0;
what can I do to use fractions correctly?
Thanks in advance.
- 09-06-2011, 09:34 PM #2
Re: trouble using fractions
Raw integers (numbers without a decimal point) are considered ints- they are only whole numbers (0, 1, 76, -45), so you lose anything after the decimal point.
Try to do it the other way: what happens when you divide a big number by a small number? Try 12/5, for example.
One solution would be to use them as raw doubles (numbers with a decimal point). Try 1.0/12.0. You could also cast one (or both) of them:
double a = ((double)1)/12
When you mix a double and an int, the answer is considered a double.
- 09-06-2011, 09:48 PM #3
- By rvgsd in forum New To JavaReplies: 2Last Post: 03-07-2010, 01:10 AM
- By SwEeTAcTioN in forum New To JavaReplies: 10Last Post: 01-13-2010, 05:51 PM
- By itaipee in forum AWT / SwingReplies: 2Last Post: 12-09-2009, 07:11 PM
- By Popedreadlock in forum New To JavaReplies: 8Last Post: 12-08-2008, 01:28 AM
- By Unknown1369 in forum New To JavaReplies: 13Last Post: 07-22-2008, 12:52 AM