-->
Page 1 of 1

Trim characters from a value gotten from BME280

PostPosted: Thu Nov 10, 2016 7:19 pm
by swilson
Using a NodeMCU1.0 board with a BME280 for a little project. The BME280 returns values for temp, humidity, and dew point with decimals such as 74.14 F. I want to trim the output of that so it just shows 74 F or at least 74.1 F. Does anyone know how I can do that? I looked throught eh BME280-master library I am using but no help. Thanks.

Re: Trim characters from a value gotten from BME280

PostPosted: Fri Nov 11, 2016 12:03 pm
by eketjall
Not an ESP8266 issue but anyway;

If you want to store the output in a char array:
Code: Select allfloat val = 74.14;
char array[10];
sprintf(array, "%.1f", val);    // %.1f  for 1 decimal, %.2f for 2 decimals etc
// array contains your "new" value as a char array.


if you just want an integer: (just truncates, no rounding)
Code: Select allfloat val = 74.14;
int new_val = (int) val;



if you just want to print to serial:
Code: Select allfloat val = 74.14;
int n = 1 // number of decimals
Serial.print(val,n);

Re: Trim characters from a value gotten from BME280

PostPosted: Sun Nov 13, 2016 12:38 am
by swilson
Thank you very much. I also found a StringRemove command.