Displaying the number with decimals not a whole number?

Hi, recently I tried to read data from an Ultrasonic flow meter using Arduino, the protocol I used is MODBUS RTU. I found some codes that help me out, some data were Integers, and I read it right but there are some data that have decimals but with the code, it only shows the whole number only.
I will show the part of the code that needed.

#define Reg_FlowRate 0

SoftwareSerial swSer;
ModbusMaster node;

float Flowrate = 0.0;

float HexTofloat(uint32_t x) {
 return (*(float *)&x);
}

void loop() {
  
  Flowrate = Read_Meter_1(ID_meter, Reg_FlowRate);

 Serial.print(Flowrate);
}
long Read_Meter_1(char addr, int REG) 
{
  long i = 0;
  uint8_t j, result;
  uint16_t data[2];
  uint32_t value = 0;

  node.begin(addr, swSer);
  result = node.readHoldingRegisters(REG, 2);
  delay(200);

  if (result == node.ku8MBSuccess) {
    for (j = 0; j < 2; j++) {
      data[j] = node.getResponseBuffer(j);
    }
    value = data[1];
    value = value << 16;
    value = value | data[0];
    i = HexTofloat(value);

    //Serial.println("connect modbus ok. ");
    return i;
 }  else {
   Serial.print("connect modbus failed");
   Serial.println(REG);
   delay(1000);
   return 0;
 }
}

Result

-3.00

in the following i is defined as a long

long Read_Meter_1(char addr, int REG) 
{
  long i = 0;
.......
    i = HexTofloat(value);

when HexToFloat() is called any fractional component is lost
define i as a float and the function should return a float

I changed it to float but nothing changed

    Serial.println (M_PI, 3);

prints

3.142

the real value is 3.56183

the result in arduino

3.000

I'm sorry, you are correct.
I forgot to change both to float
Now it read decimals, thanks.

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