Arduino rounding up numbers to print on display

Hi all,
I am making an if loop to delete the previous numbers, for example, if the number is 10 and changes to 9, otherwise the display shows 19. So if the number is less than 10 i print "space" and 9, in order to replace the previous 1 with a space and display 9.

            if (Frequency<=9.99)
              {
               lcd.setCursor(0,0);
               lcd.print("  ");
               lcd.print(Frequency);
              }/*

Now I believe the code is quite straight forward, but the arduino as a will of its own and rounds up 9.99 to 10, printing 10 on the display, rendering the loop useless.
Any workarround for this? Sure I could change 9.99 to 9.49 but I would lose all the values in between

what do you have frequency set as? it should be float

Why not leave out the 'if' and always clear out the old number?
Set cursor
Print spaces
Re-set cursor
Print data

Don

// to right justify 

lcd.setCursor(0,0);

if (Frequency<10000)
{ lcd.print(" ");}

if (Frequency<1000)
{ lcd.print(" ");}

if (Frequency<100)
{ lcd.print(" ");}

if (Frequency<10)
{ lcd.print(" ");}

lcd.print(Frequency);

Is this what you want to do?
or actual rounding of Frequency?

pity this tricks won't work for negative numbers ... e.g. Frequency = -100;

(yes I know frequency is a bad example)

pity this tricks won't work for negative numbers ... e.g. Frequency = -100;

True. Just put a test at the beginning if <0. Then do the save way, only reverse the logic.

not sure, but wouldn't this right justify the Frequency to the same spot, whether it is a negative or positive number? I haven't tested, just a thought.

Startingpos = 10 - Frequency.length();
lcd.clear();
lcd.setCursor(0,Startingpos);
lcd.print(Frequency);

if Frequency was a string that would work (length <=10)

I would go this way

lcd.setCursor(0,0);
bool neg = false;
if (Frequency < 0)
{
  neg = true;
  Frequency = abs(Frequency);
}
for (long t = 1000000; t > Frequency; t/=10)  lcd.print(' ');
lcd.print(neg ? '-' : ' ' );
lcd.print(Frequency);

I ended up doing this way:

if (Output_Freq<=9)
               {
               lcd.setCursor(0,1);
               lcd.print("  ");
               lcd.print(Output_Freq,0);
               }
            else if (Output_Freq>=10.0 && Output_Freq <=99)
               {
               lcd.setCursor(0,1);
               lcd.print(" ");
               lcd.print(Output_Freq,0);
               }
            else if (Output_Freq>=100)
               {
               lcd.setCursor(0,1);
               lcd.print(Output_Freq,0);
               }

Thank you all for sharing ideas

You have a black hole between 9 and 10

Initial if condition should be "Output_freq > 10?
Unless of course you know it will be 9 or 10 exactly

uri_ba:
You have a black hole between 9 and 10

Initial if condition should be "Output_freq > 10?
Unless of course you know it will be 9 or 10 exactly

Have a look at the first question.
The arduino is rounding up the number output to the LCD. So if the result is 9.7 it prints 10 on the LCD. Hence the loop needs to make sure that whatever he rounding the numbers will be absolute.

The above is not a solution, its a workaround. For me its enough, but I would certainly like to know why it rounds the output. The same happens on a graphical TFT (Had the same issue before).

Make Frequency a float
example:
lcd.print(Frequency, 2);
or
lcd.print(Frequency, 3);

steinie44:
Make Frequency a float
example:
lcd.print(Frequency, 2);
or
lcd.print(Frequency, 3);

Frequency is a float. I simply dont need to print anything above the decimal point on the LCD (I only need it internally for the calculations).

Copy to an int then display it. It will display 9

float Frequency = 9.97;
int Freq;
void setup() {
Serial.begin(9600);
}

void loop() {
  Freq = Frequency;
Serial.println(Freq);
}

steinie44:
Copy to an int then display it. It will display 9

float Frequency = 9.97;

int Freq;
void setup() {
Serial.begin(9600);
}

void loop() {
  Freq = Frequency;
Serial.println(Freq);
}

Yes, the issue is only while printing to LCD's. Serial works fine. Some mistery :wink:

If you copy a float of 9.97 to an int, as in my example.
What does the LCD show for the int?

steinie44:
If you copy a float of 9.97 to an int, as in my example.
What does the LCD show for the int?

Sorry I completelly missed the int bit.

Thats actually a good idea. Ill try it later