Check a float value (only one cell after the comma)

Hi guys I have a weird problem.
Ok, I have a float variable X

An example value is 1.5641

Ok, my code need to check this value but I don't want to check all the numbers of the value!

here what I would to obtain:

If (x == 1.5)
{
 ...
}

and not to be forced to write:

If (x == 1.5641)
{
 ...
}

You see I just to check ONLY until the number after the comma!

Thank you in advance!

You see I just to check ONLY until the number after the comma!

After what comma? There are no commas in 1.5641.

It sounds like you want to convert the value in the variable to a string, with one decimal place, and compare that to another string.

Or, you might test that the value is in some range:

if(X >= 1.5 && X <= 1.6)
{
   // Close enough...
}

PaulS:
After what comma? There are no commas in 1.5641.

It sounds like you want to convert the value in the variable to a string, with one decimal place, and compare that to another string.

Or, you might test that the value is in some range:

if(X >= 1.5 && X <= 1.6)

{
  // Close enough...
}

??? 1.5 -------------> 1,5 I mean the comma between 1 and 5!

Ok Paul you gived me 2 ways to obtain to check just 1.5 instead 1.5641.

But so no commands exist to directly convert a float number from a value like 1.5641 to a value like 1.5 ??

But so no commands exist to directly convert a float number from a value like 1.5641 to a value like 1.5 ??

No, because the Arduino, and every other computer on the planet, stores data in binary. Float values are only approximated. 1.5 can be approximated exactly. 1.4 can not.

aldoz:
You see I just to check ONLY until the number after the comma!

Floating point numbers have only a comma after formatting the number to a string.
If doing international formatting, there also will not be a decimal comma, but a decimal point.
In some countries like Germany a decimal comma is used more often, so for human readable output you might format strings with decimal comma instead of decimal point.

In case of floating point comparisons you usually have to define a range of the number, using a small value of 'epsilon' as the allowed variation.

So perhaps you might want to do something like:

const float epsilon=0.05;
if (x<1.5-epsilon) // value is less than 1.45
{
  // ...
}
else if (x>=1.5+epsilon) // value is equal or more than 1.55
{
  // ...
}
else  // value is in the range 1.45 and less than 1.55
{
  // ...
}

Check the avr-libc-user-manual that ships with the IDE. All the math functions are documented there.
You could do this:

if (round(x*10.0)/10 == 1.5) {}

or

if (floor(x*10.0)/10 == 1.5) {}

Is there no way in your code, that you can use > or < instead?

mm ok so I just to use your String convert advice:

  1. transform float value 1.5641 in a string
  2. transform that string from "1.5641" into "1.5"
  3. transform actual "1.5" string into a float again
  4. My new float value is "1.5"

mmm.. right?

Why not just multiply by 10 and convert to an integer?

  1. transform float value 1.5641 in a string
  2. transform that string from "1.5641" into "1.5"
  3. transform actual "1.5" string into a float again
  4. My new float value is "1.5"

mmm.. right?

It would. But, if the original value was 1.4641, converting that to a string, 1.4, and back to a float would NOT result in the value 1.4 being stored in the float. Maybe 1.400001 or 1.39999, but NOT 1.4.

mmm my damn english...

My big problem is that the value of the float X variable is not handled or set by me.

This value can be:
1.5641 or 1.61300 or 1.7923 or 1.81127 etc etc

After that, there are some events that start when these values are reached.
Example:

if (X == 1.5641) ----------> start event 1
if (X == 1.61300) ---------> start event 2
if (X == 1.7923) ----------> start event 3
if (X == 1.81127) ---------> start event 4

Just I would to obtain the following and have my program run correctly:
if (X == 1.5) ----------> start event 1
if (X == 1.6) ----------> start event 2
if (X == 1.7) ----------> start event 3
if (X == 1.8) ----------> start event 4

Or just make a math convert to obtain (example) 1.81127 in 1.8!

Aha! I suspected as much. You should do it in ranges:

if (x > 1.8) {}
else if (x >1.7) {}
else if (x >1.6) {}
else if (x >1.5) {}
//...etc

Modify to suit your needs.

Thank you all guys