Sounds So Simple RoundUp()

I have a number that is determined from some parameters for some LED's and to calculate them correctly i need to ensure the number i always rounded up to the nearest integer regardless of whether its 1.008 or 1.95 however i seem to be having great difficulty finding some code that works for this or coming up with something myself.

I tried first some of another website that involved using the modulo function but it ignored the 1.008 value and a couple others but in the end i have resorted to dare i say it floating point math.

My code i have is below however although works for some does not work for all such as in the case of 255 / 10 which returns 25.5 so should be 25 but doesn't get recognized or rounded.

Code i mashed up:

void setup() {
  Serial.begin(9600);
  Serial.println(roundUp());

}

int roundUp()
{
  float f1 = 255;
  float f2 = 10;
  if(f1/f2 - round(f1/f2) > 0){
    Serial.println("Round UP!");
    return f1/f2+1;
  }
  return f1/f2;
}

Delta_G:
To round a float variable up to the next whole number:

float someFloat = 4.2;

int roundedUp = ((int)someFloat) +1;




Casting to int will always truncate the number, that is round it down. So just add one to the number and now it's rounded up.

That will not work when the float has a fractional part of .000000, unlikely as that is.

How many decimal places of accuracy are you looking for? I'd be inclined to do the math as integers if possible, since float can sometimes give an erroneous answer such as x.000001 or x.999999 instead of precisely x.000000

I have a number that is determined from some parameters for some LED's and to calculate them correctly i need to ensure the number i always rounded up to the nearest integer regardless of whether its 1.008 or 1.95 however i seem to be having great difficulty finding some code that works for this or coming up with something myself.

That is not the normal function of round(), 1.008 would round down.

You could probably try something like round(f1/f2 + 0.4999999)

Delta_G:
To round a float variable up to the next whole number:

float someFloat = 4.2;

int roundedUp = ((int)someFloat) +1;




Casting to int will always truncate the number, that is round it down. So just add one to the number and now it's rounded up.

That will round 4.0 up to 5.0 which is not rounding up.

int roundUp()
{
  float f1 = 255;
  float f2 = 10;
  return (f1+f2-1)/f2;
}

Delta_G that looks brilliant but just tested it with 3 numbers and on 255/3 which returns a whole 85 instead i get 86.

I am only after 2 decimal places.

Sorry i am dividing two integers together that in turn produce a float which i want to round up unless it is exact such as 85.00.

Hope this makes things clearer.

(int)(yourFloat + 0.5)

1 Like

I am working out the steps of increment in an LED animation and i have this correct in an excel sheet i have attached in an image below but i use the RoundUp function which is where my issue lies.

void setup() {
  Serial.begin(57600);
  Serial.println(roundup(255, 10));
  Serial.println(roundup(255, 3));
}

void loop() {
}

int roundup(int f1, int f2) {
  //note f2 cannot be 0
  return (((f1 * 1000l) / f2) + 999) / 1000;
}

Delta_G that works perfect for the 30 different tests i have just tried.

I am also sorry if i caused confusion i don't know why it always ends up with me having to explain 3 different ways i genuinely thought about my first question and then confused everyone with the word integer.

Thank you so much i hope this helps others.

1 Like

Delta_G:
No, that won't work. With this 4.2 would give you 4. The OP want's all values between 4 and 5 to go to 5.

Oh didn't read properly ^^

I appreciate your help although i must once again say it strangely disslikes the two numbers 40/23 and although this equals 1.73 it returns a 0 in the double !! section and as such doesnt round up.

I must say that i dont quite understand that section of the code and as such having trouble working out why this is happening.

Any help?

Please post a complete, small sketch that clearly demonstrates the problem. That will give people something concrete to work with.

Deep Sigh

Yes you would be correct.

void setup() {
  Serial.begin(9600);
  Serial.println(calculate(40, 23));

}

int calculate(int num1, int num2){
  int output = num1 / num2 + !!(num1%num2);
  return output;
}

In fact upon typing this out i discovered that instead of % i had put an & well atleast its here for everyone to see now and working, thank you for your help.