# Thread: Double vs Int as output in Java

1. Member
Join Date
Feb 2013
Posts
3
Rep Power
0

## Double vs Int as output in Java

Hi
I am in my first Java Programming class and I am having the followintg problem:

I am constructing a program to show how many years and days are contained in 1 billion minutes.

Here is my code:

import java.util.Scanner;

public class Ex02_07 {
public static void main (String [] args) {
Scanner input = new Scanner(System.in);
//Prompt the user for input
System.out.print("Enter a number of minutes ");
double minutes = input.nextDouble();
double years = minutes / 5256000.0;
double remainder = years % 365.0;

System.out.println("There are approximately " + years +
" years and" + remainder + " days.");
}
}

This produces the following output:

C:\Users\Owner\Desktop\JavaPractice>javac Ex02_07.java

C:\Users\Owner\Desktop\JavaPractice>java Ex02_07
Enter a number of minutes 10000000000

There are approximately 1902.5875190258753 years and 77.58751902587528 days.

C:\Users\Owner\Desktop\JavaPractice>

I need the decimal part in order to figure how many days are left after the number of years are computed, but I do not want the decimal part to show in the output statement in either the years portion nor the days portion. Also the days should be approximately 214: not 77.58751902...

What am I missing? Can someone please point me in the right direction?

Thanks,
BettyS

2. ## Re: Double vs Int as output in Java

• First off, you're using "magic" numbers and should avoid doing that. What does 5256000.0 mean? If it is the product of numbers, show the numbers in order to help make your code self-commenting.
• Next, if you need to do int division, do int division and not double division.
• Next, if you want to show the remainder of a division, the numerator and denominator for the division are usually the same first and 2nd numbers in a mod operation to calculate the remainder. Your numbers are different -- why?
• Please use [code] [/code] tags when posting code on this forum so that we can read it more easily.

3. Member
Join Date
Feb 2013
Posts
3
Rep Power
0

## Re: Double vs Int as output in Java

I am in my first Java Programming class and I am having the followintg problem:

I am constructing a program to show how many years and days are contained in 1 billion minutes.

Here is my code:

Java Code:
``` import java.util.Scanner;

public class Ex02_07 {
public static void main (String [] args) {
Scanner input = new Scanner(System.in);
//Prompt the user for input
System.out.print("Enter a number of minutes ");
double minutes = input.nextDouble();
double years = minutes / 525600.0; // number of minutes in years is 525600
double remainder = years % 365.0; //number of days in year is 365

System.out.println("There are approximately   " + years +
" years and" + remainder + "    days.");
}
}```
This produces the following output:

C:\Users\Owner\Desktop\JavaPractice>javac Ex02_07.java

C:\Users\Owner\Desktop\JavaPractice>java Ex02_07
Enter a number of minutes 10000000000

There are approximately 1902.5875190258753 years and 77.58751902587528 days.

C:\Users\Owner\Desktop\JavaPractice>

I need the decimal part in order to figure how many days are left after the number of years are computed, but I do not want the decimal part to show in the output statement in either the years portion nor the days portion. Also the days should be approximately 214: not 77.58751902...

What am I missing? Can someone please point me in the right direction?

Thanks,
BettyS

Hi Fubarable:

I hope my edited post above is easier to read and understand. :)

BettyS

4. ## Re: Double vs Int as output in Java

You still have magic numbers. I suggest that you use constants so that your calculations look something more like:

Java Code:
```      int minutes = 1000 * 1000 * 1000;
System.out.println("billion = " + minutes);

int hours = minutes / MINUTES_PER_HOUR;```
Where MINUTES_PER_HOUR is a constant defined in the program as
Java Code:
`private static final int MINUTES_PER_HOUR = 60;`
With similar constants for the equivalent days and years calculations. Then calculate the remainder from *each* calculation of this type that you do. Again, the two function parameters (here the numerator and denominator) must be the same for division and for mod operation.

5. Member
Join Date
Feb 2013
Posts
3
Rep Power
0

## Re: Double vs Int as output in Java

Originally Posted by Fubarable
You still have magic numbers. I suggest that you use constants so that your calculations look something more like:

Java Code:
```      int minutes = 1000 * 1000 * 1000;
System.out.println("billion = " + minutes);

int hours = minutes / MINUTES_PER_HOUR;```
Where MINUTES_PER_HOUR is a constant defined in the program as
Java Code:
`private static final int MINUTES_PER_HOUR = 60;`
With similar constants for the equivalent days and years calculations. Then calculate the remainder from *each* calculation of this type that you do. Again, the two function parameters (here the numerator and denominator) must be the same for division and for mod operation.
Thanks Fubarable:

I think that is what I needed. :)
BettyS