Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By superhaNds

Thread: Can't get java to display decimal numbers

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

    Default 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:
    package hoursslept;
    import java.util.Scanner;
    public class HoursSlept {
    
        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. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default 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. #3
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Default 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. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

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

    Quote Originally Posted by BigBlueBuffalo View Post
    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.
    itkremit likes this.

  5. #5
    765891 is offline Member
    Join Date
    Jan 2013
    Location
    'Murica
    Posts
    25
    Rep Power
    0

    Default 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. #6
    765891 is offline Member
    Join Date
    Jan 2013
    Location
    'Murica
    Posts
    25
    Rep Power
    0

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

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

  7. #7
    jpil is offline Member
    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0

    Default 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;
        }

Similar Threads

  1. Replies: 8
    Last Post: 03-02-2013, 01:38 AM
  2. using decimal numbers
    By iminhell in forum New To Java
    Replies: 3
    Last Post: 10-31-2010, 03:47 AM
  3. Replies: 4
    Last Post: 01-16-2009, 06:17 PM
  4. Truncating decimal numbers in the output
    By gbade in forum New To Java
    Replies: 2
    Last Post: 11-21-2008, 06:25 PM
  5. numbers with two decimal places
    By little_polarbear in forum New To Java
    Replies: 8
    Last Post: 08-27-2008, 11:04 PM

Posting Permissions

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