routine counts wrong

Hi!
I'm using this routine to display a integer on the LCD.
But it counts wrong when the integer is more than 100
(displays 100 as 101 and 101 as 102 etc) :frowning:
can somebody help me to get it right?

// checks out how many digits there are in a number
int estimateDigits(int nr) {
  int dec = 10;
  int temp = 1;
  int div = nr/dec;
  while (div > 0) {
    dec *= 10;
    div = nr/dec;
    temp++;
  }
  return temp;
}

// shows numbers on the display
void LcdNumberWrite(int nr) {
  int digits = estimateDigits(nr);
  LcdNumberWrite(nr, digits);
}

// this function help us to write numbers 
// with more than one digit
void LcdNumberWrite(int nr, int digits) {
  for (int i = digits-1; i >= 0; i--) {
    int dec = pow(10,i);
    int div = nr/dec;
    lcd.print(div+48); 
    if (div > 0) {
      nr -= div*dec; 
    }
  }
}

I've had similar problems when converting numbers to string. I used doubles in my function, and got rounding errors. Can it be something like that for you too?

Have you thought of using the modulo operator "%"?

@Hans K

rounding errors.

A really good point. I always cringe when I see people using floating point operations (the pow function) to multiply ints.

To Bosen: Just a WAG, but what happens if you do this?
Change the following line

        int dec = pow(10, i);

to

        int dec = pow(10, i) + 0.5; // Round it rather than truncate

Regards,

Dave