how to correctly display a number on Nokia 5110?

This should be easy for anyone but me. My coding skills a pretty much limited to cobbling other peoples work together and hope to make it work. I might be using some incorrect terminology here, but bear with me please.
I'm trying to display a value "Altitude" on the LDC. It will display correctly in serial monitor, (257.XX) but I get random numbers on the LCD. I think it's got something to do with the decimal vs char. I don't need to see the decimals on the LCD. The string (dtostrf(Altitude,0,0,altitude_arr)) prints out on serial monitor with three digits and no decimal. I've got no idea how to print that out on the LCD.

Thanks,
Chris

     String str_out =                                                                 //combine all values and create part of NMEA data string output
       String("LK8EX1"+String(",")+String(average_pressure,DEC)+ String(",")+String(dtostrf(Altitude,0,0,altitude_arr))+String(",")+
       String(dtostrf((vario*100),0,0,vario_arr))+String(",")+String(my_temperature,DEC)+String(",")+String(Battery_Vcc,DEC)+String(","));
     unsigned int checksum_end,ai,bi;                                                 // Calculating checksum for data string
     for (checksum_end = 0, ai = 0; ai < str_out.length(); ai++)
     {
       bi = (unsigned char)str_out[ai];
       checksum_end ^= bi;
     }
    // creating now NMEA serial output for LK8000. LK8EX1 protocol format:
    // $LK8EX1,pressure,altitude,vario,temperature,battery,*checksum
    // Serial.print(dtostrf(Altitude,0,0,altitude_arr));
    // Serial.print(" ,,,, " );
     [color=red] Serial.print(my_temperature);    [b]  // celcius, two digits.  no decimal [/b][/color]
      Serial.print(" , " );
   [color=red]   Serial.print(Altitude);          [b] // meters,    257.45  [/b]    [/color]
      Serial.print(" , " );
     [color=red] Serial.println(dtostrf(Altitude,0,0,altitude_arr));  [b] //  meter  257  no decimal[/b][/color]
    
     get_time3 = millis();
      
     LcdClear();
     gotoXY(1,1);
     LcdString("TEMP C");
     char buffer_t[8];
     sprintf(buffer_t, "%4d", my_temperature, DEC);
   [color=red]  LcdString(buffer_t);                            [b] // TEMP reads out properly on LCD[/b][/color]

    gotoXY(1,3); 
    LcdString("ALT M ");
    char buffer_alt[8];
    sprintf(buffer_alt, "%4d",Altitude, DEC );      
   [color=red] LcdString(buffer_alt);                     [b]  // LCD displays random numbers positive and negative.[/b][/color]
         
     
     
     
     
 }
 }
 //The End

Is Altidude defined as a float?

i.e.:

float Altitude;

If so, you could try either:

sprintf(buffer_t, "%4f", my_temperature);
LcdString(buffer_t);

or

sprintf(buffer_t, "%4d", (int)my_temperature);
LcdString(buffer_t);
The DEC currently on the end of your sprintf is doing nothing, and is ignored.

Thanks arduinolb!
Altitude is a float.
Changing sprintf(buffer_alt, "%4d", Altitude ); to sprintf(buffer_alt, "%4d", **(int)**Altitude ); did it. I'm guessing that (int) causes it to be sent as an integer, not as a float?

It casts it to an integer, yes. Google "C cast" for more information.

     [color=red] Serial.print(my_temperature);    [b]  // celcius, two digits.  no decimal [/b][/color]
      Serial.print(" , " );
   [color=red]   Serial.print(Altitude);          [b] // meters,    257.45  [/b]    [/color]
      Serial.print(" , " );
     [color=red] Serial.println(dtostrf(Altitude,0,0,altitude_arr));  [b] //  meter  257  no decimal[/b][/color]

That code is hard to read. Next time please just copy the code and paste it inside code tags, don't use "copy for forum" no matter how tempting that might be. :slight_smile:

chris13409:
Thanks arduinolb!
Altitude is a float.
Changing sprintf(buffer_alt, "%4d", Altitude ); to sprintf(buffer_alt, "%4d", **(int)**Altitude ); did it. I'm guessing that (int) causes it to be sent as an integer, not as a float?

Short answer, yes. It "casts" it as a float which in this case, causes it to be converted to an int before "sending" it as a parameter (technically, adding it to the stack). The parameter types have to match those inside the sprintf format string or strange things can happen, as you saw.