# Int to String Issues

• 09-21-2010, 05:33 PM
xael
Int to String Issues
Hello,
I'm trying to make a program that can calculate pi to several digits using the madhava sequence. The math part is irrelevant to my problem though. I want to calculate pi beyond 16 digits, which is the most precise form of numeric storage (in the variable type 'double'). So, I am going to save it as a string as it goes. For some reason though, I cannot convert the number into a string.
Code:

```//the whole class/program  package pigenerator; public class Main {     static String pistring = " ";     static double pinum = 0;     static int denominator = 1;     static int limit = 2000000000;     static boolean add = true;     static int digits = 0;     public void Main() {     }     public static void main(String[] args) {         findpi();     }     public static void findpi() {// based on madhava series         while (denominator < limit) {             boolean toggle = add;             if (add == true) {                 pinum += 4 / denominator;                 add = false;             }             if (toggle == false) {                 pinum -= 4/ denominator;                 add = true;             }             denominator += 2;             digits++;                         if(digits>4)             {                 pistring+=pinum.toString();// here occurs the error             digits=0;                         }             System.out.println(pistring);                     }     }         }```
The idea is that it calculates a few digits, and then adds it to the string. When I say
Code:

`    pistring+=pinum.toString();`
I get an error saying 'double cannot be dereferenced'. Any ideas about what might be causing this error are greatly appreciated. Also, don't worry about the math or anything other than the 'toString()' line. I know that the rest is all fine. Thank you so much.
• 09-21-2010, 05:39 PM
JosAH
A type double is a so called 'primitive' type; primitives don't have any members nor methods so you can't dereference them. Lucky for you the String class has a valueOf( ... ) method that returns a String representation given a (primitive) double value.

kind regards,

Jos
• 09-21-2010, 06:21 PM
xael
Easy solution...thanks!
• 09-21-2010, 07:14 PM
Zack
JosAH, could you not also use:
Code:

```int x = 5; String xAsStr = x+"";```
...?

Is that less efficient, or bad practice, or what?
• 09-21-2010, 07:33 PM
JosAH
Quote:

Originally Posted by Zack
JosAH, could you not also use:
Code:

```int x = 5; String xAsStr = x+"";```
...?

Is that less efficient, or bad practice, or what?

Sure you can but it's less efficient: the compiler creates a StringBuffer for you, adds the "" String and x for you and finally extracts the String from that buffer for you while you could've done it directly by using the method I mentioned in my previous reply (although I'm ofther lazy and use ""+x myself ;-)

kind regards,

Jos
• 09-21-2010, 08:00 PM
Zack
Good to know. I'll keep that in mind, thanks. (""+x is much easier for quick code, but I guess valueOf() should be used in effective programming, specifically where allocation is an issue.)
• 09-21-2010, 08:08 PM
JosAH
Quote:

Originally Posted by Zack
Good to know. I'll keep that in mind, thanks. (""+x is much easier for quick code, but I guess valueOf() should be used in effective programming, specifically where allocation is an issue.)

Yep and I should take my own advice to heart because there are ""+x's in my own code all over the place ;-)

kind regards,

Jos