 # Decimal Point Problems. How to make a variable value of 1.35?

In the code below, lets use 300 as input on pin A0.

``````int inputVoltagePin = A0; // Which is 300
int divBy = 204.00;
const int LED1 = 9;

void setup()
{
Serial.begin(9600);
pinMode(LED1, OUTPUT);
}

void loop()
{
int valueVolts = voltage / divBy; // 300 / 204.00 = 1.470

Serial.println(valueVolts); // prints 1 instead of 1.470

if(valueVolts > 1.35) { analogWrite(LED1, 220); } // even though the value is greater than 1.35, still does not enter because its reading only 1
else { analogWrite(LED1, 0); }
delay(100);
}
``````

Just in case you did not catch the problem, in the if else statement, the value is greater than 1.35, but, still does not enter the if because the variable “valueVolts” is 1 instead of 1.470.
How can I make this to not only print 1.470 but the value itself to be 1.470?

Your using an 'int' variable that can only hold whole numbers (no fractions). Try changing 'int valueVolts' to 'float valueVolts' as the 'float' type can hold floating point values (with fractions).

int is a whole number, float is a decimal number (have a look through the Data Types list here: http://arduino.cc/en/Reference/HomePage )
When you do

``````int valueVolts = voltage / divBy;
``````

It automatically rounds down the value, which is why you get 1.

Solved! I replaced the int's that had decimal point/values by float's. I had 2 days wondering what the problem might be... silly problem.

You Guys Rock! THANKS!

You can use floats, fixed-point math, or smaller units and print the decimal place only in output.

I was a math and science head since the 60's so to me it's no problem choosing units to use integers with. Maybe it's an outcome of using pencil and paper for so many years but the decimal point is just a reference to me.

If I want to measure meters down to 4 decimal places then I make my unit micrometers. I take care to do my divisions last and have two places that can round before the 4th place digit is compromised. For me, two thirds of a 123456 is 123456 * 3 / 2 and if I want to chase it then the remainder is 123456 * 3 % 2. If 123456 is 'really' 123.456 and I need 2 place accuracy then I don't need to chase the remainder.

I can go with floats but on an AVR they will be teeth-grinding slow and in the process of doing much math with them I can count on errors that I won't get using integers. But... they will be small errors. When .9999999 really is 1 and process speed is not critical then go ahead and use floats.