# Adding Decimals and Taking Away Decimals?

Hey so I'm tying to make a small project. The code is on the bottom. What I want it do do is to check to see if xval is equal to 5. If so turn on the LED. Here's my problem. When open up serial monitor, it prints all these decimals. I don't want these decmials, is there a way to make it go in whole numbers, 1,2,3 etc. Is there a way to make it round off to the nearest number? And finally, If I do this,

`````` voltage = (xval * 2.54);
``````

It only give me a whole number. How do I make it give a decimal, say two decimal places?

``````int LED = 11;
void setup()

{
pinMode(LED, OUTPUT);
Serial.begin(9600);
}

void loop()
{
float xval = pot * (10.0 / 1023.0);
Serial.println(xval);

if(xval == 5)
{
analogWrite(LED, 255);
}

else
{
digitalWrite(LED, LOW);
}

}
``````

It only give me a whole number

How is voltage defined? If it is an int, that is to be expected.

How do I make it give a decimal, say two decimal places?

Define it as a float. You will get whatever accuracy is possible. Rounding to two decimal places is a printing function.

try , quickest way:

``````int xval = int ( pot * (10.0 / 1023.0) );

int voltage = int ( xval * 2.54 );
``````

What do you mean, define it as a float? I need to actually see it. Mistergreen, can you explain what that code is for and how will it solve my problems.

What do you mean, define it as a float?

Like this

Exact comparison of floating point values is often a Bad Ideatm because of the way they are represented. You can take the absolute value of the difference, and see if that is smaller than some threshold instead.
Or use fixed-point.

Instead of floats, you should get into the habit of working in smaller values, and use integers.

For instance, instead of working in volts (2.54), work in millivolts (2540).

You can then divide it by 1000 at the last moment for displaying if you want volts, but the mathematics of integers over floating point numbers is both considerably faster, and uses considerably less flash / memory.

Sluice: What do you mean, define it as a float? I need to actually see it. Mistergreen, can you explain what that code is for and how will it solve my problems.

It casts/ convert floats to integers. Simply put, floats are numbers with decimals. Intergers are whole numbers.

Do what majenko suggests.