Remove number before decimal

Hello, I need little help. In my project, I have float number like 0.54 and i need print .54

How can I do this? Already tried convert float to string and remove function, but that is not working for me :frowning:

My actual code here:

void ProgOut(byte id, float val) {
  switch (id) {
    case 5: lcd1.setCursor(0, 0);  if (val >= 1) {
        lcd1.clear(); lcd1.print("SPD"); lcd1.setCursor(0, 1); if (val < 100) lcd1.print('0');
        if (val < 10) lcd1.print('0'); lcd1.print(int(val));
      } else {
        lcd1.setCursor(0, 0); if (val < 1) lcd1.clear();  lcd1.print("MCH"); lcd1.setCursor(0, 1); lcd1.print(val);
      }
  }
}

Would something like this work for you?

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

void loop()
{
  float val = 0.54;
  int tmp = val * 100; // after moving decimal point 2 places right
  Serial.println(tmp); // converting to integer throws away
                       // the fraction (truncates)
  while(1); // press reset button to repeat
}

Not work for me :( i received from 0.54 this: 54 and two chinese chars :D i need decimal number with point, but without leading zero. This number never exceed 0,99.

After this

lcd1.setCursor(0, 1);

try this:

int valh = val * 100;                // multiply by 100 and cast to int
if (valh>=100) lcd1.print(valh/100); // print whole part if necessary
lcd1.print('.');                     // decimal point
if ((valh%100)<10) lcd1.print('0');  // zero for decimals .00 through .09
lcd1.print(valh%100);                // hundredths

You need to add

Serial.print('.');

in the appropriate place, and properly post your current code.

The Serial.print('.') will give you the missing decimal point. I need to see your code to understand why you are seeing the Chinese characters. Perhaps the Chinese characters were already on the screen?

Ah, formating working well now, but value is not updating… only value after else updating (data stream with value working well).

case 4:
      if (val < 1) {
        lcd1.setCursor(0, 1);
        int valh = val * 100;                
        if (valh >= 100) lcd1.print(valh / 100);
        lcd1.print('.');
        if ((valh % 100) < 10) lcd1.print('0');
        lcd1.print(valh % 100);
      }
      else {
        lcd1.setCursor(0, 1);
        if (val < 100) lcd1.print('0');
        if (val < 10) lcd1.print('0');
        lcd1.print(int(val));
      }
      break;

How can one possibly help except as a guess? In the snippet of code shown, there is no updating, and there is val and valh but no value.

Please post the ENTIRE code using the instructions below.

To post code and/or error messages:

  1. Use CTRL-T in the Arduino IDE to autoformat your complete code.
  2. Paste the complete autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.
    3) Paste the complete error message between code tags (the </> button)
    ~~ so that we can easily see and deal with your messages.~~
  3. If you already posted without code tags, you may add the code tags by
    ~~ editing your post.~~ Do not change your existing posts in any other way.
    You may make additional posts as needed.
  4. Please provide links to any libraries that are used
    (look for statements in your code that look like #include ). Many libraries
    are named the same but have different contents.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a clear photograph of the wiring.

Good Luck!

#include <Wire.h>
#include <LiquidCrystal_I2C.h>    // download the library here: https://github.com/marcoschwartz/LiquidCrystal_I2C
//================================
#include <ArdSimX_Interface.h>          //  -- ArdSimX library http://svglobe.com/ardsimx/files/ArdSimX_Interface.zip
//================================


LiquidCrystal_I2C lcd1(0x27, 16, 2); // i2c address 0x27
LiquidCrystal_I2C lcd2(0x25, 16, 2); // i2c address 0x27
LiquidCrystal_I2C lcd3(0x23, 16, 2); // i2c address 0x27

//------------------------------------------
void setup()  {

  BoardNumber 1;            // -- Assign Board Number here  (0...9)

  lcd1.begin();
  lcd1.backlight();
  lcd2.begin();
  lcd2.backlight();
  lcd3.begin();
  lcd3.backlight();
}

//------------------------------------------
void loop()   {

  ArdSimScan;               // main loop  - scan inputs and read incoming data for output

}
//===========================================



void ProgOut(byte id, float val) {
  switch (id)
  {
    case 1:
      lcd1.setCursor(0, 0);
      if (val == 0) lcd1.print("SPD");
      if (val == 1) lcd1.print("MCH");
      break;

    case 2:

      if (val == 0) {
        lcd1.setCursor(5, 0);
        lcd1.print("HDG");
        lcd1.setCursor(9, 0);
        lcd1.print("LAT");
        lcd1.setCursor(13, 0);
        lcd1.print("HDG");
        lcd1.setCursor(13, 1);
        lcd1.print("V/S");
        break;
      }
      else if (val == 1) {
        lcd1.setCursor(5, 0);  lcd1.print("TRK"); lcd1.setCursor(9, 0); lcd1.print("LAT"); lcd1.setCursor(13, 0); lcd1.print("TRK"); lcd1.setCursor(13, 1); lcd1.print("FPA");
        break;
      }

    case 3:
      lcd1.setCursor(9, 1);
      if (val == 1) lcd1.print(" * ");
      if (val == 0) lcd1.print("   ");
      break;

    case 4:

      if (val < 1) {
        lcd1.setCursor(0, 1);
        int valh = val * 100;
        if (valh >= 100) lcd1.print(valh / 100);
        lcd1.print('.');
        if ((valh % 100) < 10) lcd1.print('0');
        lcd1.print(valh % 100);
      }
      else {
        lcd1.setCursor(0, 1);
        if (val < 100) lcd1.print('0');
        if (val < 10) lcd1.print('0');
        lcd1.print(int(val));
      }
      break;

    case 5:
      lcd1.setCursor(5, 1);
      float val2 = round(val);
      if (val2 < 100) lcd1.print('0');
      if (val2 < 10) lcd1.print('0');
      lcd1.print(int(val2));
      break;



  }
}

Few photos of LCD with values. SPD value updating OK, MCH value not. If i remove your code, MCH value will update OK.

spd_value_from_source.png

mach_value_from_source.png

It has gotten too deep for me. I quit.

This cannot be the code you use. This won't compile

  BoardNumber 1;            // -- Assign Board Number here  (0...9)

and this won't do anything useful

void loop()   {

  ArdSimScan;               // main loop  - scan inputs and read incoming data for output

}

Those are macros in ArdSimX_Interface.h which turn into something that can compile. I almost responded about these but then reviewed the library files and was straightened out.