10 Replies - 2387 Views - Last Post: 18 August 2009 - 04:07 PM Rate Topic: -----

#1 boredom   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 03-January 09

Return the No. of decimal places

Post icon  Posted 13 August 2009 - 06:01 AM

Hi, I need to write a Java Program that asks user to input the number of digits up to which he/she wants the program to return the value of PI.

off course the output is value of PI up-to the required no. of decimal places.
e-g:

Input:
Enter the no of decimal places up-to which you want the value of PI evaluated: 5(say)
Output:
The value of PI upto 5 decimal places is : 3.14XXX

Continue with the program (Y/N)
If Y:
then:
Enter the no of decimal places up-to which you want the value of PI evaluated:
if N:
then program should Quit.


Please give your Ideas as to hoe to proceed, I am new to programming

Is This A Good Question/Topic? 0
  • +

Replies To: Return the No. of decimal places

#2 DaneAU   User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,620
  • Joined: 15-May 08

Re: Return the No. of decimal places

Posted 13 August 2009 - 06:14 AM

[rules][/rules]

On another note, please check out String.format("%.2f", value); which will print a value to 2 decimal places. Likewise String.format("%.4f", value); will print to 4 decimal places.

double p = 3.142425233;

System.out.println( String.format("%.2f", p ) );
// prints 3.14


This post has been edited by bbq: 13 August 2009 - 06:15 AM

Was This Post Helpful? 0
  • +
  • -

#3 boredom   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 03-January 09

Re: Return the No. of decimal places

Posted 15 August 2009 - 03:36 AM

View Postbbq, on 13 Aug, 2009 - 05:14 AM, said:

[rules][/rules]

On another note, please check out String.format("%.2f", value); which will print a value to 2 decimal places. Likewise String.format("%.4f", value); will print to 4 decimal places.

double p = 3.142425233;

System.out.println( String.format("%.2f", p ) );
// prints 3.14



A part of my code is as follows:
Scanner input = new Scanner(System.in);
	
	//prompt the user to enter a number
	System.out.println("Enter the no. of digits in the value of PI, upto you wanna see the result");
	int n = input.nextInt();
	NumberFormat nf = new DecimalFormat("0.000000000000000");
	
	if(n == 2) {
		System.out.println("Value of Pi upto two decimal places is : 3.14");
	}
	else if(n >2) {
		System.out.println("Value of Pi is: " +nf.format(PI));
	}


I know this does not do what I am trying to do.
Can you tell me how do I get around to return the specific no. of digits in the value of PI.
Thanks for Help.
Was This Post Helpful? 0
  • +
  • -

#4 AntonWebsters   User is offline

  • D.I.C Regular
  • member icon

Reputation: 89
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Return the No. of decimal places

Posted 15 August 2009 - 04:57 AM

Maybe you can try using BigDecimal and its setScale method to get it done?

final double PI = 3.142425233;
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
BigDecimal p = new BigDecimal(PI);
System.out.println("The value of PI is " + p.setScale(n,BigDecimal.ROUND_UP));


Was This Post Helpful? 0
  • +
  • -

#5 boredom   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 03-January 09

Re: Return the No. of decimal places

Posted 15 August 2009 - 05:11 AM

View PostAntonWebsters, on 15 Aug, 2009 - 03:57 AM, said:

Maybe you can try using BigDecimal and its setScale method to get it done?

final double PI = 3.142425233;
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
BigDecimal p = new BigDecimal(PI);
System.out.println("The value of PI is " + p.setScale(n,BigDecimal.ROUND_UP));



Thanks a Ton for the Help.
First of all I would like you to Welcome DIC. Anyway I sorted out my first issue with your code fragment.
Can you please tell me why I do I have to initialize the PI, if it is the inbuilt constant in Java.
When I declared the PI as
final double PI;
or
public static final double PI

I got the error as:"The local variable PI may not have been initialized"

Thanks Again.
Was This Post Helpful? 0
  • +
  • -

#6 DaneAU   User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,620
  • Joined: 15-May 08

Re: Return the No. of decimal places

Posted 15 August 2009 - 06:07 AM

That means PI has not might not be anything when the program executes.

try double PI = 0; and it should remove the issue.

This post has been edited by bbq: 15 August 2009 - 06:07 AM

Was This Post Helpful? 0
  • +
  • -

#7 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Return the No. of decimal places

Posted 15 August 2009 - 08:53 AM

View Postboredom, on 15 Aug, 2009 - 11:11 AM, said:

Thanks a Ton for the Help.
First of all I would like you to Welcome DIC. Anyway I sorted out my first issue with your code fragment.
Can you please tell me why I do I have to initialize the PI, if it is the inbuilt constant in Java.
When I declared the PI as
final double PI;
or
public static final double PI

I got the error as:"The local variable PI may not have been initialized"

Thanks Again.

if you are OK with PI as a doble it is defined in the java.math API, e.g.
import java.math.*;
 ...
System.out.println("PI " + Math.PI);


prints
PI 3.141592653589793


This post has been edited by horace: 15 August 2009 - 08:54 AM

Was This Post Helpful? 0
  • +
  • -

#8 AntonWebsters   User is offline

  • D.I.C Regular
  • member icon

Reputation: 89
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Return the No. of decimal places

Posted 15 August 2009 - 10:17 AM

View Postboredom, on 15 Aug, 2009 - 04:11 AM, said:

View PostAntonWebsters, on 15 Aug, 2009 - 03:57 AM, said:

Maybe you can try using BigDecimal and its setScale method to get it done?

final double PI = 3.142425233;
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
BigDecimal p = new BigDecimal(PI);
System.out.println("The value of PI is " + p.setScale(n,BigDecimal.ROUND_UP));



Thanks a Ton for the Help.
First of all I would like you to Welcome DIC. Anyway I sorted out my first issue with your code fragment.
Can you please tell me why I do I have to initialize the PI, if it is the inbuilt constant in Java.
When I declared the PI as
final double PI;
or
public static final double PI

I got the error as:"The local variable PI may not have been initialized"

Thanks Again.


So, the code worked? I couldn't check if it's working or not just now, cause I was on Ubuntu, and something went wrong with my Eclipse and Netbeans. Thanks for the welcome, and um, you could've just use Math.PI instead of using final double PI = 3.142425233.
Was This Post Helpful? 0
  • +
  • -

#9 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Return the No. of decimal places

Posted 15 August 2009 - 01:01 PM

I think this is what you are looking for:

String str = "0.";
for(int i = 0; i < nbDigit; i++)
	str += "0";
DecimalFormat nf = new DecimakFormat(str);
System.out.println(nf.format(Math.PI));


Was This Post Helpful? 0
  • +
  • -

#10 boredom   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 03-January 09

Re: Return the No. of decimal places

Posted 18 August 2009 - 02:51 AM

View Postpbl, on 15 Aug, 2009 - 12:01 PM, said:

I think this is what you are looking for:

String str = "0.";
for(int i = 0; i < nbDigit; i++)
	str += "0";
DecimalFormat nf = new DecimakFormat(str);
System.out.println(nf.format(Math.PI));






I don't need a loop, I just need one iteration for the time being, That will return the value of PI upto that no. of decimal places:
e-g n = 34
output = 3.141592653589793..........................(upto 34 decimal places)


I also don't want round up.

This post has been edited by boredom: 18 August 2009 - 02:59 AM

Was This Post Helpful? 0
  • +
  • -

#11 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Return the No. of decimal places

Posted 18 August 2009 - 04:07 PM

View Postboredom, on 18 Aug, 2009 - 01:51 AM, said:

View Postpbl, on 15 Aug, 2009 - 12:01 PM, said:

I think this is what you are looking for:

String str = "0.";
for(int i = 0; i < nbDigit; i++)
	str += "0";
DecimalFormat nf = new DecimakFormat(str);
System.out.println(nf.format(Math.PI));






I don't need a loop, I just need one iteration for the time being, That will return the value of PI upto that no. of decimal places:
e-g n = 34
output = 3.141592653589793..........................(upto 34 decimal places)


I also don't want round up.

The loop is to build your number format to display PI with the number of decimals you want
That only works with float and double

As far of 34 decimal is concerned Math.PI is a double so should have 15 or 16 decimal digits
So you will have to write your own function to evaluate PI using a comninaison of BigDecimal and BigNumber
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1