# Thread: Can't get java to display decimal numbers

1. Member
Join Date
Aug 2011
Posts
5
Rep Power
0

## Can't get java to display decimal numbers

I'm creating this little program and I need it to give me the decimal number, but it keeps giving me 0.0.

Any help would be great. Thanks!

Java Code:
import java.util.Scanner;

public static void main(String[] args) {

Scanner myScanner = new Scanner(System.in);

int totalHoursLeftToLive;
int totalHoursOfSleepLeft;
int totalYearsLeftToLive;
double percentageOfSleep;

System.out.println("How many years do you expect to live for?: ");
totalYearsLeftToLive = myScanner.nextInt();
totalHoursLeftToLive = totalYearsLeftToLive * 8765;

System.out.println("How many hours do you sleep a day?: ");
totalHoursOfSleepLeft = myScanner.nextInt();
totalHoursOfSleepLeft *= 365 * totalYearsLeftToLive;

percentageOfSleep = totalHoursOfSleepLeft / totalHoursLeftToLive;

System.out.println("You will sleep for " +percentageOfSleep+ "% of your remaining life");
System.out.println("You have " +totalHoursOfSleepLeft+ " of sleeping hours left in your life");
System.out.println("You also have " +totalHoursLeftToLive+ " total hours left in your life");

}

}

2. Senior Member
Join Date
Apr 2013
Location
Sweden
Posts
272
Rep Power
5

## Re: Can't get java to display decimal numbers

you have to typecast to double because you are dividing integers

var = (double) x / (double) y

3. Member
Join Date
Aug 2011
Posts
5
Rep Power
0

## Re: Can't get java to display decimal numbers

Thanks.

Do you know how I can limit the amount of numbers after the decimal point? It's displaying something long like 0.23456464654646432% and I want it only to display 2, maybe 3, numbers after the decimal point.

4. Senior Member
Join Date
Apr 2013
Location
Sweden
Posts
272
Rep Power
5

## Re: Can't get java to display decimal numbers

Originally Posted by BigBlueBuffalo
Thanks.

Do you know how I can limit the amount of numbers after the decimal point? It's displaying something long like 0.23456464654646432% and I want it only to display 2, maybe 3, numbers after the decimal point.
You can use a decimal format

google DecimalFormat java and check the documentation on how to do it.

5. Member
Join Date
Jan 2013
Location
'Murica
Posts
25
Rep Power
0

## Re: Can't get java to display decimal numbers

Hello,
you can use printf (printf will round to whatever digit you place after the . and between the f, and yes you need a comma after the quotation marks, you also need to enter\n for a new line since it doesn't print a line down)

System.out.printf("type what ever here.2f%",double)

replace double with the variable that need conversion/ rounding to a specific digit

6. Member
Join Date
Jan 2013
Location
'Murica
Posts
25
Rep Power
0

## Re: Can't get java to display decimal numbers

System.out.printf("\nYou will be sleeping for .2f%",percentageOfSleep );

7. Member
Join Date
Dec 2013
Posts
2
Rep Power
0

## Re: Can't get java to display decimal numbers

I sometimes use a method like the one below,

Java Code:
public static double limitDecimalPlaces(double d, int numberOfPlaces){
int i = 1;
for (int x = 0 ; x < numberOfPlaces ; x++ ){
i *= 10;
}
d *= i;
d = Math.round(d);
d /= i;
return d;
}

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•