Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: For loop-ing decimal format

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default For loop-ing decimal format

    Hi all, I'm trying to find a way to use a for-loop to determine the number of decimal places that my program will output to. Specifically I want the the decimal place to be determined at run time by the user from a Scanner input following a prompt for a number. So if the user inputs 3 the program only outputs to "0.###".

    So i guess i need to somehow make a DecimalFormat that adds a "#" to the back of it itself until it reaches the user's input (that's where the for-loop comes in)? I need some help with the specifics of writing though...

    Many thanks in advance.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: For loop-ing decimal format

    need to somehow make a DecimalFormat that adds a "#" to the back of it itself until it reaches the user's input (that's where the for-loop comes in)
    Yes that sounds like you could use a loop to concatenate #s to a String until you have what the user requested.
    Do you know how to write a loop?
    Do you know how to concatenate a String onto another String?

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: For loop-ing decimal format

    You dont have to actually loop to do this. DecimalFormat has a method called setMaximumFractionDigits(int). So, we can simply use this to set the number of places. Here is a snippet of code I made and tested.
    WHERE:
    d is our double Variable we are rounding
    places is the number of decimal places
    df is our DecimalFormat
    double d = 9.987656;
        	int places = 3;
        	DecimalFormat df = new DecimalFormat();
        	df.setMaximumFractionDigits(places);
        	System.out.println(df.format(d));

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: For loop-ing decimal format

    Quote Originally Posted by Norm View Post
    Yes that sounds like you could use a loop to concatenate #s to a String until you have what the user requested.
    Do you know how to write a loop?
    Do you know how to concatenate a String onto another String?

    I'm pretty new to java, i can write a loop but i don't know how to concatenate a string.

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: For loop-ing decimal format

    wouldn't i have to change the value of int places in the coding to change the number of decimal places with this method though?

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: For loop-ing decimal format

    Yes, but the int place value would be the value the user inputs for the number of decimal places. So you would want to set places to whatever the user inputs for the number of decimal places. So that code would look more like this (I think I used Scanner correctly, but I'm not sure):
    System.out.println("Input Number of Decimal Places: ");
    Scanner scan = new Scanner(System.in);
    int places = scan.nextInt();
     
    double d = 9.987656;
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(places);
    System.out.println(df.format(d));

    Also, keep in mind that if the double has less decimals then the user submitted, it will not print out those extra 0s. If you did want to print out those extra 0s, you would also have to set the Minimum Fraction Digits by doing both of these statements:
    df.setMaximumFractionDigits(places);
    df.setMinimumFractionDigits(places);
    Last edited by aussiemcgr; October 1st, 2010 at 09:35 AM.

  7. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Harj (October 1st, 2010)

  8. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: For loop-ing decimal format

    Thanks a lot, code works like a charm your way.

    I tried to code it as a for-loop as well just for practice, and I got that to work kind of. It rounds to desired decimal places, but I can't find an easy way to get rid of the null on the end of those...


    String pattern = "0.";
    String decis = null;

    //now we're setting up the Scanner to read user inputs

    Scanner input = new Scanner(System.in);

    /*the written out prompts for program's ease of use and setting
    input results as usable variables
    */

    System.out.println ("please input a number to square.");

    number = input.nextDouble();

    System.out.println ("to how many places?");

    places = input.nextInt();

    //the math operation to be carried out by the program,
    //could have been more complex if desired

    square = (number * number);

    //for-loop to concatenate the string "decis" "place" # of times to itself


    for (decimal = 1; decimal <= places; decimal++ ){
    decis += ("#");
    }

    //DecimalFormat made by concatenating 2 strings
    DecimalFormat fmt = new DecimalFormat (pattern + decis);

    System.out.println ("the square of " + (number) + " is " + fmt.format(square));
    }
    }
    there's definitely something more elegant than writing a line to delete "null" I'm sure.

  9. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: For loop-ing decimal format

    Give an example of the output. I'm having trouble visualizing where the null is.

  10. #9
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: For loop-ing decimal format

    formerly i would get outputs like so:

    please input a number to square.
    564.2212
    to how many places?
    1
    the square of 564.2212 is 318345.6null
    just changed

    String decis = null;

    to

    String decis = "";

    runs look a lot better now:

    please input a number to square.
    3.2839276
    to how many places?
    3
    the square of 3.2839276 is 10.784

  11. #10
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: For loop-ing decimal format

    That is interesting. You'd think when you set decis to null, when you tried to add to it you would get a compiler error or something, but it seems to just concatinate to it as if null was the String set to it. Hmm.

  12. #11
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: For loop-ing decimal format

    I thought setting it to null was the same as not giving it a value? that way only the #s would appear later.

    either way I'm done with this, your original version solves all so thanks again.

Similar Threads

  1. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Programming Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 04:08 PM
  2. print hex decimal value
    By ran830421 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 25th, 2009, 07:23 PM
  3. decimal and hexdecimal
    By ran830421 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 21st, 2009, 02:58 AM
  4. decimal to hex
    By rsala004 in forum Algorithms & Recursion
    Replies: 1
    Last Post: November 3rd, 2009, 02:16 AM
  5. [SOLVED] How to use decimal place when formatting an output?
    By napenthia in forum Java Theory & Questions
    Replies: 2
    Last Post: April 27th, 2009, 03:17 AM

Tags for this Thread