# Return the No. of decimal places

Page 1 of 1

## 10 Replies - 2387 Views - Last Post: 18 August 2009 - 04:07 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=119992&amp;s=83fdc9ec8459132e4f659871d6bf13af&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 boredom

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

# Return the No. of decimal places

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

• Great::Southern::Land

Reputation: 286
• 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

### #3 boredom

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

## Re: Return the No. of decimal places

Posted 15 August 2009 - 03:36 AM

bbq, 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.

### #4 AntonWebsters

• D.I.C Regular

Reputation: 89
• 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));

```

### #5 boredom

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

## Re: Return the No. of decimal places

Posted 15 August 2009 - 05:11 AM

AntonWebsters, 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.

### #6 DaneAU

• Great::Southern::Land

Reputation: 286
• 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

### #7 horace

• D.I.C Lover

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

## Re: Return the No. of decimal places

Posted 15 August 2009 - 08:53 AM

boredom, 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

### #8 AntonWebsters

• D.I.C Regular

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

## Re: Return the No. of decimal places

Posted 15 August 2009 - 10:17 AM

boredom, on 15 Aug, 2009 - 04:11 AM, said:

AntonWebsters, 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.

### #9 pbl

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

Reputation: 8379
• 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));

```

### #10 boredom

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

## Re: Return the No. of decimal places

Posted 18 August 2009 - 02:51 AM

pbl, 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

### #11 pbl

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

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

## Re: Return the No. of decimal places

Posted 18 August 2009 - 04:07 PM

boredom, on 18 Aug, 2009 - 01:51 AM, said:

pbl, 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