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

## Convert a double to and integer

**Moderators:** imaqine, 99jonathan, roger

### Re: Convert a double to and integer

Try this

Code: Select all

`MotorA.setPower((int)PowerA);`

My NXT blog: http://nxttime.wordpress.com/

### Re: Convert a double to and integer

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);

### Re: Convert a double to and integer

Or

Code: Select all

` double x = 5.43212345;`

int i = (int)Math.round(x);

System.out.println(""+i);

### Re: Convert a double to and integer

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);

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);

### Who is online

Users browsing this forum: Baidu [Spider] and 2 guests