Hello - I have created a program which calculates the value of PI. All the calculations work well but the aim of the program is to get the user to input a number - say 7 and my program will print the value of PI to 7 decimal places.
I realise there is a DecimalFormatter and I used to 8 decimal places, for e.g.
// Method to format output to 8 decimal places
public static String toEightString( double quantity )
{
DecimalFormat currQuant = new DecimalFormat("#.#########");
return currQuant.format( quantity );
}
But this isn't feasible if the user enters 100 - Im not going to create a hundred methods like the above! So I thought of converting the value of PI, of type double, to a float and then using the formatter like below:
float toDP = (float)quantity;
int totalNum = numDP+1;
System.out.printf( "%totalNum.numDPf", toDP );
So this was suppose to print, say user enters 4, 5 numbers in total with 4 past the decimal place. But when I call it it returns an error.
So what is the best way to format the output?
Cheers