# how do i divide a smaller number by a larger one?

the question sounds simple enough. for example 2 / 30 = .067. but when i do it in arduino, i keep getting 0.

sample code:

``````int duration = 30; //as in 30 seconds. let's not worry about the milliseconds right now
int timePassed = 2; as in 2 seconds
double percentageOfDurationPassed;

void setup()
{
percentageOfDurationPassed = timePassed/duration;
// 2 / 30 = .067
Serial.print(percentageOfDurationPassed);
}

void loop()
{

}
``````

i keep getting 0 for percentageOfDurationPassed, while i expect to bet a .067.

any thoughts? maybe i'm just making a newb mistake here. thanks!

`shields

INT 2/INT 30 = INT 0 maybe?

You need to cast the integers to floats before the division like this:

``````percentageOfDurationPassed = (double) timePassed / (double) duration;
``````

Alternativly, if you want to know the percentage, as opposed to the fraction, you can prescale: `percentageOfDurationPassed = (100*timePassed)/duration`This will only show to nearest whole percent, of course. (If you understand the "of course" you've become wiser in the difference between integer math and float math :))

Integers are used for working with whole numbers and even then within the limits of the number of bits for that type. Floating point math is used for working with mixed numbers.

Lefty

You need to cast the integers to floats before the division like this:

``````percentageOfDurationPassed = (double) timePassed / (double) duration;
``````

doh. i should have remembered that. it's been a good while since i learned java.

big thanks to you and everyone who replied. i think i'm on the right track! pretty new to arduino, but i'm completely diggin' it. thanks again. `shields