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

Thread: How to change it to only one decimal?

  1. #1
    mackanmorre is offline Member
    Join Date
    Apr 2017
    Posts
    12
    Rep Power
    0

    Default How to change it to only one decimal?

    Hey here is the code:

    Java Code:
    public class Uppgift3c
    {
        public static void main (String[] args)
        {
            
            double number = 2.0;
            
            
            while (number > 0.5)
            {
                number -= 0.1;
                System.out.println(number);
            }
            
            System.out.println("Finished");
            
        }
    }
    This program is supposed to print out everything from 2 to 0.5 with one decimal, for example 2...1,9......1,8 etc. But the problem in this case is that it prints out number like 1,9999999.......1,8999999999 etc. How can i format number variable so it only prints out 1 decimal?

    thanks!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to change it to only one decimal?

    appears on like 12.5010.50,
    Look at using the PrintStream's printf mehtod. It allows a format String that defines what the format of the output will be.
    See the API doc for the Formatter class for how to build format Strings.

    Also the String class's format() method will do formatting.
    mackanmorre likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to change it to only one decimal?

    Your post also indicates you want a different decimal separator than a period. Do the following for the appropriate country.

    For example:
    Java Code:
    Locale.setDefault(Locale.GERMANY);
    Then your decimals will print out like 1,9 instead of 1.9.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    mackanmorre is offline Member
    Join Date
    Apr 2017
    Posts
    12
    Rep Power
    0

    Default Re: How to change it to only one decimal?

    Hey,

    I have tried this one :

    Java Code:
    System.out.printf("%.0f",number);
    I have also tried a bunch of other options, but I just can get it to work. Do you know what could be wrong?

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to change it to only one decimal?

    Can you post what you have tried and also what was printed from them?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    mackanmorre is offline Member
    Join Date
    Apr 2017
    Posts
    12
    Rep Power
    0

    Default Re: How to change it to only one decimal?

    I used the same line as I wrote. I tried changing ("%.0f",number); to a bunch of other such as ("%.1f",number);

    The result I was getting was that it prints out 2 as long as the number is closer to 2 than 1, and then it starts printing 1.

    I also tried this method now, and it works for the first few numbers. Here is the code:

    Java Code:
    public class Uppgift3c
    {
        public static void main (String[] args)
        {
            
            double number = 2.0;
            
            
            while (number > 0.5)
            {
                number -= 0.1;
                number= number*10/10;
                System.out.println(number);
            }
            
            System.out.println("Finished");
            
        }
    }
    This one prints out 1.9 1.8 1.7, but then it starts printing 1.599999 and 1.4999999 and so on.

  7. #7
    mackanmorre is offline Member
    Join Date
    Apr 2017
    Posts
    12
    Rep Power
    0

    Default Re: How to change it to only one decimal?

    I solved it!

    Here is the solution

    Java Code:
    public class Uppgift3c
    {
        public static void main (String[] args)
        {
            
            double number = 2.0;
            
            
            while (number > 0.5)
            {
                number -= 0.1;
                System.out.printf("%8.1f", number);//Gör så att endast en decimal skrivs ut
            }
            
            System.out.println("Finished");
            
        }
    }

Similar Threads

  1. Replies: 0
    Last Post: 08-05-2015, 08:47 AM
  2. Not getting decimal for a surely decimal answer
    By Avionix in forum New To Java
    Replies: 1
    Last Post: 01-01-2015, 05:45 AM
  3. Replies: 1
    Last Post: 11-19-2013, 10:25 PM
  4. Replies: 5
    Last Post: 08-25-2011, 07:32 PM
  5. Decimal to binary, octal to decimal
    By matejm1994 in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 09:59 AM

Tags for this Thread

Posting Permissions

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