How do I get a float number to decimal places ?

Hi,
I have .. float heat_index = 20.09435789

I want this number to have 2 decimal places ... 20.09

I do NOT want to print it to serial as I know you can manipulate it their.
It needs to be a number and not text.

I did try dtostrf(DHT_01_heat_index,4,1,Buffer);
Then convert that to a float but I just got the long numbers after decimal point again.

Any simple methods ?

Thanks

Multiply by 100, take the integer value, divide by 100.

Hi,

Not sure if this is what you meant ?

float heat_index = 20.09435789 * 100;
int X = heat_index / 100;

This gives the answer 20

Thanks

    float heat_index = 20.09435789;
    int x = heat_index * 100;
    float y = (float)x / 100;
    Serial.println(y, 6);

Thanks,

That gives me a actual number 20.09000015

I want to send this to a gauge and I really need just 2 decimal places ... 20.09
Is this at all possible, as i have looked for an hour and cannot find an answer for Arduino ?

An Arduino float is always going to have 6 or 7 decimal places. A string representation of a float can have any number of decimal places, or at least up to 7

Which Arduino board are you using ?

try

float heat_index = 20.09435789 * 100;
int X = heat_index; 
heat_index = X;  
heat_index = heat_index/100;

but you don't need the int. Try,

  float heat_index = 20.09435789;
  heat_index = (int)(heat_index*100);
  heat_index = heat_index / 100;

But, you say
"send this to a gauge"
via what method? The reason you have a tiny residual on the float is because of representation limitations in the datatype float. That'll be similar in any system, and should not be of concern.
Why does your "gauge" insist on showing residuals of irrelevancy?

Hi,

I am using a ESP8266.

I have misunderstood what float does in Arduino, I thought you could change it to as few decimal places as I liked, as I have used the above as text and the used dtostrf.

But I can see that in Arduino it is fixed to 8 decimal places.

This is going to be sent out to Node-RED gauge and a database. This does work in Node-RED

msg.payload = Number(msg.payload.toFixed(2))
return msg;

So I assume whenever we use a float that we keep the full number and manipulate the answer in what ever device or software we use ?

Thanks

No, it's not. The float type is fixed to the IEEE 754 32-bit format. On ESP8266 you can also get IEEE 754 64-bit format by using 'double'. dtsotrf() will give an approximate ASCII representation of the float to a specified number of decimal places. With ESP8266, you can also use sprintf() to do this..

1 Like

That's the nature of floating point values, they are in base 2, not base 10, so converting to a base 10 representation is often inexact.

Usually you only restrict the number of digits or places when printing a value for humans, the internal representation is kept un-truncated.

1 Like

Try dividing by 100.0 so the result is a floating point number.

a7

No, not with any normal computer. A float can represent a number with 6 to 7 total digits of accuracy. It is not possible to represent the exact value of all numbers with a given number of decimal places.

For example, 0.1 cannot be represented exactly by a float value. I suspect that your example of 20.09 cannot be represented exactly, either.

Just print out the value to two decimal places after the decimal point.

how are you sending the value?
ascii string, integer bytes, the bytes representing the float value, ...?

OK thanks guys, I have decided to change to 2 decimal places in Node-RED, as it has the following which works on my gauge in Node-RED.

msg.payload = Number(msg.payload.toFixed(2))
return msg;

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