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”

Werbung


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.

Sources:

Published by

Uli

IT-Nerd und Admin

Leave a Reply

Your email address will not be published. Required fields are marked *