Convert a double to and integer

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

Moderators: 99jonathan, roger, imaqine

Convert a double to and integer

Postby jdhigh2 » Sun Nov 27, 2011 10:45 pm

I need to convert a double to an integer. I have some some information on-line about how to do this, but nothing that is step-by-step enough to figure it out.
I am working on PID control for a line follower bot.
I have calculated my motor power control value as a double.
Now I need to convert it to an integer for use in: MotorA.setPower(PowerA);

Any help would be greatly appreciated.
Thanks,
John
jdhigh2
New User
 
Posts: 2
Joined: Sun Nov 27, 2011 2:29 am

Re: Convert a double to and integer

Postby Aswin » Sun Nov 27, 2011 10:58 pm

Try this
Code: Select all
MotorA.setPower((int)PowerA);
My NXT blog: http://nxttime.wordpress.com/
Aswin
leJOS Team Member
 
Posts: 199
Joined: Tue Apr 26, 2011 9:18 pm
Location: Netherlands

Re: Convert a double to and integer

Postby roger » Sun Nov 27, 2011 11:00 pm

All you have to do is cast the double to int. Example:
Code: Select all
      double x = 5.43212345;
      int i = (int)x;
      System.out.println(""+i);
roger
Moderator
 
Posts: 359
Joined: Fri Jun 01, 2007 4:31 am
Location: Berkeley, CA

Re: Convert a double to and integer

Postby skoehler » Mon Nov 28, 2011 3:01 am

Or
Code: Select all
      double x = 5.43212345;
      int i = (int)Math.round(x);
      System.out.println(""+i);
skoehler
leJOS Team Member
 
Posts: 1405
Joined: Thu Oct 30, 2008 4:54 pm

Re: Convert a double to and integer

Postby sioux » Tue Nov 29, 2011 10:12 am

Or you can use the Math.round(float a) method that returns a rounded int in case you are interested in that.
You will have to cast the double as a float in the round's constructor like this.

double i = 5.72;
int p = Math.round((float)i);
sioux
New User
 
Posts: 8
Joined: Sun Nov 27, 2011 4:19 pm


Return to NXJ Software

Who is online

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

more stuff