Type Conversion from Int to Float in the Arduino Code

A couple days ago i played around with a SI7021 on my ESP8266 and recognized that the temperature output of the used Library was given as an integer, so without a comma or dot for the separation of the decimal places. Like 2495 instead of 24,95°C. Not really problematic, but for using the numbers with MQTT it was relevant to convert it to the proper type which was “Float”


That is how this works:

float tempfloat;
int tempint = 2495;
tempfloat = (float) tempint / 100.0;

Take care to add a decimal place to the division. The Usage of (float) executes the casting which changes the type of the variable content.


Published by


IT-Nerd und Admin

Leave a Reply

Your email address will not be published.

I accept that my given data and my IP address is sent to a server in the USA only for the purpose of spam prevention through the Akismet program.More information on Akismet and GDPR.