How do I write this IF statement?

Hi I have a 4 digit integer with a decimal in this format: XX.XX

How do I write this statement?

IF XX.X1 THEN ?

in other words I want to specify a condition if the first digit is a one and so on

thanks :slight_smile:

Sorry, but your question makes no sense at all!
xx.xx isn’t an integer, integers don’t have fractional (decimal) parts.
xx.x1 is the 4th digit, fifth character.
What are yo actually trying to achieve?
Do you have any code already ?

Hello and good morning,
Take a look here :nerd_face:

You could try something like:

void setup() 
{
  double test1 = 45.81;
  double test2 = 33.22;
  Serial.begin(115200);

  Serial.print(test1); 
  if (String(test1, 2).endsWith("1"))
    Serial.println(" is a match");  
  Serial.println(" is NOT a match");

  Serial.print(test2); 
  if (String(test2, 2).endsWith("1"))
    Serial.println(" is a match");  
  Serial.println(" is NOT a match");
}

As has been pointed out it is not an integer
Where does it come from and what data type is it declared as ?
Is it by any chance a string of characters representing a number rather than an actual number ?

I'm using the TMP36 library, I believe the variable is float not integer unless there is another data type to represent decimals.

I wrote a basic multiplexing scheme for a 4 digit display, I want an IF statement like

IF variable1 = xx.x1 THEN function1

Explain what do you really want to do, and forum members can show you how to do it.

1 Like

Meaning you have an integer and you want to display it with a decimal point?

Post the code.

To print a floating point number with two digits after the decimal, most people multiply by 100 to convert it to an integer. Send the integer to the display and turn on the correct decimal point.

if (variable1 = =xx.x1)
{
digitalWrite(PC13, HIGH); //function1
}

  1. you are doing an assignment not comparison
  2. you cannot compare floats/doubles with == it is unreliable at best

Can you please confirm that given a float variable with 2 significant decimal places then you want to test whether the digit in the second decimal place is a 1 ?

Thanks everyone for the help ill try to rephrase..

Variable1 is now a 4 digit number (I have taken a users advice and multiplied by 100 to remove the decimal). Using the set of 40 conditions I have written (0-9, for a 4 digit 7 segment display) I want to call the appropriate function (1 of 40) depending on the value of each digit in the number and pass to the display via multiplexing.

Examples: where the first number is the digit and the second number is the value

IF variable1 = 3523 THEN function4-3, function3-5, function2-2, function1-3

IF variable1 = 9999 THEN function4-9, function3-9, function2-9, function1-9

for simplicity I would like to have a statement that looks at any digit (1 to 4) and calls the appropriate function based on the value (0-9) instead of doing all four digits at once

BOGGLE !

40 functions to drive a 4 digit display ?

Are you serious ?

I'm new to Arduino and I'm trying to build on what I already understand

my multiplexing scheme works great and if I can solve this problem I'm satisfied...

maybe you can point me to an excellent arduino guide because they all stink

Well the OP could have settled on 280 functions, one for every segment…

a7

Easy:
if ((((int)(number * 100)) % 10) == 1)

josnwasser Can you elaborate on the syntax of this command? What is int and number?

Well, 'number' is the name of the variable containing the value. I assume it is type 'float' or 'double'.
'(int)' is a 'cast'. It tells the compiler to convert (number * 100) to an integer. Your float number 'ab.cd' becomes an integer 'abcd'.
The 'modulo' operator (%) does the modulo operation. Modulo 10 is a way of isolating the right-most decimal digit.

thanks! Here is my solution for all interested parties

var1 = 12.34;
var2 = var1 * 100;

  if ((((var2)) % 10) == 4) {
  Serial.println("is 4");
}
else {
  Serial.println("not 4");
}
delay (5);

if ((((var2 / 10)) % 10) == 3) {
  Serial.println("is 3");
}
else {
  Serial.println("not 3");
}
delay (5);

if ((((var2 / 100)) % 10) == 2) {
  Serial.println("is 2");
}
else {
  Serial.println("not 2");
}
delay (5);

if ((((var2 / 1000)) % 10) == 1) {
  Serial.println("is 1");
}
else {
  Serial.println("not 1");
}
delay (5);