TM1638plus.h Displaying Float values

Hi all

now I have googled this but not found the solution i am looking for.

I have a float value that I wish to display on an TM1638 screen using the TM1638plus.h Library
I would like to display only 2 decimal places on the screen

The code below is one example that is provided but I one dont understand it very well and its not great when the value of float goes above 100.

Can anyone help perhaps show me an simplier way display a float to two decimal places onto a TM1638 Screen?

Thank you
Brendan

  float voltage = 12.45;
  uint16_t temp = 0;
  char workStr[11];
  uint8_t  digit1, digit2, digit3 , digit4;
  voltage =  voltage * 100; // 1245
  temp = (uint16_t)voltage;
  digit1 = (temp / 1000) % 10;
  digit2 = (temp / 100) % 10;
  digit3 = (temp / 10) % 10;
  digit4 =  temp % 10;

  sprintf(workStr, "ADC=.%d%d.%d%d", digit1, digit2, digit3, digit4);
  tm.displayText(workStr); //12.45.VOLT
  delay(myTestDelay3);
  tm.reset();

I correctly understood that the method works, but you don't like something about it? What exactly?
Multiplying a number by 100 and then integer division into digits is the standard way for such displays.

Thanks. I did think that mutily by 100 to get the value is what i was thinking in my head.
if this is the best way to do it I will stick with it.
Can you perhaps direct me to a resource that helps me understand the %10 remainder terminology better?
as this is my hurdle.

updated
Output:

ADC = 12.45
void setup ()
{
    Serial.begin (9600);

    float voltage = 12.45;
    char  t [20];
    dtostrf (voltage, 5, 2, t);
    char  s [30];
    sprintf (s, "ADC = %s", t);
    Serial.println (s);
}

void loop ()
{
}

alternatively

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

    float f = 12.45;
    int   i = 100 * f;
    char  s [20];

    sprintf (s, "ADC = %d.%d", i / 100, i % 100);
    Serial.println (s);
}

void loop ()
{
}

You can do not store the multiplied value in the float itself, but used it immediately

float voltage = 12.45;
  char workStr[11];
  uint8_t  digit1, digit2, digit3 , digit4;
  uint16_t  temp =  voltage * 100;                // 1245
  digit1 = (temp / 1000) % 10;
  digit2 = (temp / 100) % 10;
  digit3 = (temp / 10) % 10;
  digit4 =  temp % 10;

In this case the method will not affect the value of float variable.

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