Displaying a human friendly frequency

Hi,
I am retreiving a frequency in kHz with leading zeros via Serial.
I would like to display it on a LCD in a more human friendly format:

012345678 -> 12,345.678 kHz
000012345 -> 12.345 kHz

I have no idea on how to achieve this, maybe with dividing by 1000 then printing a float ?
But LCD takes a string as input.
Thanks for your help

Post the code that shows how you are getting this frequency.

Without knowing what you have, it's hard to tell you how to work with it to display it like you want.

We could guess, you could save time by posting a complete it compiles and runs sketch we could play with.

Either your actual project code, or a reduced examlpe showing the data in its current format.

a7

Sure! Here is the code I am working on:

#include "LiquidCrystal_I2C.h"

LiquidCrystal_I2C LCD(0x27,16,2);

void setup() {
   Serial.begin(19200);
   LCD.init();
   LCD.backlight();
   LCD.clear();
}

void loop(void) {
   if (Serial.available()) {
      String str = Serial.readStringUntil(';');
   }

   if (str.startsWith("F")) {		// Frequency in kHz prefixed by F (F012345678 for 12,345.678 kHz)
      String str_freq = str.substring(1,9);
      LCD.setCursor(0, 0);
      // str_freq to humanize
      LCD.print(str_freq);
   }
}
  unsigned long freq = str_freq.toInt();
  if (freq < 1000)
  {
    LCD.print(freq);
    LCD.print(" Hz");
  }
  else   if (freq < 1000000ul)
  {
    LCD.print(freq/1000.0, 3);
    LCD.print(" kHz");
  }
  else
  {
    LCD.print(freq/1000000.0, 6);
    LCD.print(" MHz");
  }

GHz?

In a 9-digit integer?

The input is string

Sorry... In a 9-digit string?

Where did you get this specific limit from?

Maybe from

      String str_freq = str.substring(1,9);

a7

Input: str = “0001234” 
Output: 1234 
Explanation: 
Removal of leading substring “000” modifies the string to “1234”. 
Hence, the final answer is “1234”.

Input: str = “00000000” 
Output: 0 
Explanation: 
There are no numbers except 0

Remove leading zeros from a Number given as a string - GeeksforGeeks

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