I am trying to make a rounding function

I am trying to make a rounding function that rounds to the nearest hundred, but when I tried it, it only outputs 0.

int roundrl() {
  rl = 200;
  if (rl < 100, rl > -1) {
    rl = 0;
  } else if (rl < 200, rl >99) {
    rl = 100;
  } else if (rl < 300, rl > 199) {
    rl = 200;
} else if (rl < 400, rl > 299) {
  rl = 300;
  } else if (rl < 500, rl > 399) {
    rl = 400;
  } else if (rl < 600, rl > 499) {
    rl = 500;
  } else if (rl < 700, rl > 599) {
    rl = 600;
  } else if (rl < 800, rl > 699) {
    rl = 700;
  } else if (rl < 900, rl > 799) {
    rl = 800;
  } else if (rl < 1000, rl > 899) {
    rl = 900;
  } else if (rl < 1100, rl > 999) {
    rl = 1000;
  }
}

What each else if is meant to do is to check if rl is below a number and above another.

there are line breaks, for some reason they didn't appear though

I doubt that that if structure is doing what you think. The comma does not belong in an if structure. Maybe && (logical and) is what you need there instead.

Try this with some different numbers:

int number = 450;

void setup()
{
   Serial.begin(115200);
   int tempNumber = number / 100;
   tempNumber = tempNumber * 100;
   Serial.println(tempNumber);
}

void loop()
{
}

Thanks for posting code properly on your first post. Few first time posters bother to read the guidelines. Good job.

You should use && instead of the comma.
The program should look like this

int roundrl(int rl) {
  if (rl < 100 && rl > -1) {
    rl = 0;
  } else if (rl < 200 && rl > 99) {
    rl = 100;
  } else if (rl < 300 && rl > 199) {
    rl = 200;
  }
  else if (rl < 400 && rl > 299) {
    rl = 300;
  } else if (rl < 500 && rl > 399) {
    rl = 400;
  } else if (rl < 600 && rl > 499) {
    rl = 500;
  } else if (rl < 700 && rl > 599) {
    rl = 600;
  } else if (rl < 800 && rl > 699) {
    rl = 700;
  } else if (rl < 900 && rl > 799) {
    rl = 800;
  } else if (rl < 1000 && rl > 899) {
    rl = 900;
  } else if (rl < 1100 && rl > 999) {
    rl = 1000;
  }
  return rl;
}

Thank you guys so much, when I first posted this it didn't show up as normal code for some reason, but now it is like that. That's why I put the reply there are line breaks there.

what about

#include <stdio.h>

int
main ()
{
    for (int i = 210; i < 300; i += 10)  {
        printf (" %6d %6d\n", i, 100 * ((i + 50) / 100) );
    }

    return 0;
}

which produces

    210    200
    220    200
    230    200
    240    200
    250    300
    260    300
    270    300
    280    300
    290    300

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.