How can I increment a number by .25


New to Arduino but finding my way.

I have a counter that stores temeperature , i have a button that increases it by 1 at the moment using ++ counter.

How do i increase it by for example .25 so that i can go from 25 centigrade to 26 in stages 25.25 , 25.50 etc.



You need to define the number as a float. Then you can increment it simply by:- float number = 0;

number = number + 0.25;

Thanks Mike

Knew it would be easy but searched and couldn't find the answer.



number += 0.25;

Thanks AlphaBeta

Good to know both ways


Just to pre-empt your next question. Due to rounding errors you might not always get a clean print out of the number. That is 23.25 could actually be printed out as 23.24999999 or it might not. This is normal. If you don't want this to happen then keep your numbers as int but at 100 times the value so that instead of 23.25 the number is actually 2325. Then convert it as you print it out:- int number = 2300;

number += 25; Serial.print( (float)number/100.0);

Great Tip

Thanks once again


An alternative would be to use integers to count the number of quarter degrees, and increment the count by one. Depending on what you want to do with them you could store them as integers or divide by four using a float variable. Might be handy to use a byte variable if you have a limited range of temperatures and your storage is a bit thin (like the built in eeprom). It world work nicely between - 32 and + 32 (or 0 and 64) just using one byte of storage.

1 Like