# For loop-ing decimal format

• October 1st, 2010, 09:40 AM
Harj
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.
• October 1st, 2010, 09:51 AM
Norm
Re: For loop-ing decimal format
Quote:

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?
• October 1st, 2010, 09:54 AM
aussiemcgr
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
Code java:

```double d = 9.987656; int places = 3; DecimalFormat df = new DecimalFormat(); df.setMaximumFractionDigits(places); System.out.println(df.format(d));```
• October 1st, 2010, 10:05 AM
Harj
Re: For loop-ing decimal format
Quote:

Originally Posted by Norm
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.
• October 1st, 2010, 10:09 AM
Harj
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?
• October 1st, 2010, 10:29 AM
aussiemcgr
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):
Code java:

```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:
Code java:

```df.setMaximumFractionDigits(places); df.setMinimumFractionDigits(places);```
• October 1st, 2010, 03:02 PM
Harj
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...

Quote:

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.
• October 1st, 2010, 03:05 PM
aussiemcgr
Re: For loop-ing decimal format
Give an example of the output. I'm having trouble visualizing where the null is.
• October 1st, 2010, 03:10 PM
Harj
Re: For loop-ing decimal format
formerly i would get outputs like so:

Quote:

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:

Quote:

please input a number to square.
3.2839276
to how many places?
3
the square of 3.2839276 is 10.784
• October 1st, 2010, 04:52 PM
aussiemcgr
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.
• October 1st, 2010, 06:47 PM
Harj
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.