How to convert a double value into string

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

How to convert a double value into string

Postby esmetaman » Sat Mar 08, 2008 6:56 pm

Hi,

I am trying the method random in Math class, and it gets a double value. I need to convert a double value into string, but Double class doesnt allow me to convert with method toString.

Code: Select all
import lejos.nxt.*;

public class RandomTest {

   public static void main (String[] aArg)  throws Exception
   {
      double a = Math.random();
      String b = a.toString();
      LCD.drawString(b,3,4);
      Thread.sleep(2000);
   }   
}
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
https://github.com/jabrena/livingrobots
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 299
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby esmetaman » Sat Mar 08, 2008 7:01 pm

Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
https://github.com/jabrena/livingrobots
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 299
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby lawrie » Sat Mar 08, 2008 9:02 pm

Code: Select all
String b = "" + a;


is probably the simplest. If you prefer, you can use StringBuffer append explicitly. Note that in lejos double is the same as float. We could add toString to Float, but there is not much point in adding Double to leJOS.
lawrie
leJOS Team Member
 
Posts: 929
Joined: Mon Feb 05, 2007 1:27 pm

Postby bbagnall » Sun Mar 09, 2008 5:34 pm

I added Lawrie's code to Float.toString() so it will be in the next version of NXJ.
User avatar
bbagnall
Site Admin
 
Posts: 392
Joined: Fri Aug 04, 2006 4:03 pm


Return to NXJ Software

Who is online

Users browsing this forum: Google [Bot] and 3 guests

more stuff