# Recover the value of the first digit after the decimal point

Hi

I can get the value of units and decimals with this command :

dezenaTemp = temp / 10;

but I would like to recover the value of the first digit after the decimal point

how to do ?

but I would like to recover the value of the first digit after the decimal point

What temperature sensor are you using? Does it really give you temperatures that have more than one digit after the decimal point?

``````float temp = 23.4;

int tempAsInt = (temp + 0.05) * 10; // tempAsInt will be 234
int firstFractionDigit = tempAsInt % 10; // value will be 4
``````

esloch:
Hi

I can get the value of units and decimals with this command :

dezenaTemp = temp / 10;

but I would like to recover the value of the first digit after the decimal point

how to do ?

``````float temp = 12.345;
int result = temp * 10; // 123.45 is truncated to int 123
result = result % 10; // 3
``````

PaulS:
What temperature sensor are you using? Does it really give you temperatures that have more than one digit after the decimal point?

``````float temp = 23.4;
``````

int tempAsInt = (temp + 0.05) * 10; // tempAsInt will be 234
int firstFractionDigit = tempAsInt % 10; // value will be 4

I use Si7021 sensor ... great ! ... but I don't know who many digit after decimal point I can get with this sensor olf2012:

``````float temp = 12.345;
``````

int result = temp * 10; // 123.45 is truncated to int 123
result = result % 10; // 3

try this ... I think It will work fine
Thanks

but I don't know who many digit after decimal point I can get with this sensor Then why did you choose that sensor? If you can't read the datasheet, post a link to it, and one if us will read it for you.

PaulS:
Then why did you choose that sensor? If you can't read the datasheet, post a link to it, and one if us will read it for you.

I use at the start the DHT11 ... very bad ... I was advised to use Si7021 ... very professional ...

Thanks

OK

I try this code :

``````int temp;

dezenaTemp = temp / 10;
centmTemp = (temp * 10) % 10;
``````

he always return the value 0 in the centmTemp ...

if I change it like this :

``````float temp;

dezenaTemp = temp / 10;
centmTemp = (temp * 10) % 10;
``````

I have this error message : invalid operands of types 'float' and 'int' to binary 'operator%'   I don't see much difference between the 2 blocks of code.

But, anyway, post all the code you have written. From the parts you have posted it is not clear how/into what you have read the temperature sensor.

Also say what degree of precision you would like see in the results.

6v6gt:
I don't see much difference between the 2 blocks of code.
...

but the first code I no have error message ...

6v6gt:
...
But, anyway, post all the code you have written. From the parts you have posted it is not clear how/into what you have read the temperature sensor.

Also say what degree of precision you would like see in the results.

OK

but my code is too long ... if you want I will post it

``````int temp;

dezenaTemp = temp / 10;
centmTemp = (temp * 10) % 10;
``````

I want a precision at the 1st digit after the point (23.4°)

with this code I can't read the 4 ... I always read it 0 ! I'm moving slightly
I displayed on the monitor different values to understand the operation
with this command

If I display this command

``````Serial.println(sensor.readTemperature(), 2)
``````

the result is (for exemple) : 22.45

If I display this command

``````int temp
Serial.println(temp, 2)
``````

the result is (for exemple) : 10110

the prob (if I understand correctly)
this command no work with the first code

``````unidadeTemp = temp % 10;
dezenaTemp = temp / 10;
centmTemp = (temp * 10) % 10;
``````

finaly work fine (but with very bad commands)

``````int temp, temp2, umid, unidadeTemp, dezenaTemp, centmTemp;

dezenaTemp = temp / 10;
centmTemp = temp2 % 10;
`````` OK. You could attach the whole code as a file.

You could also do your calculation something like this if you want to cover the full range of the sensor (up to 125 deg Centigrade) and retain a precision of one place of decimal

``````int temp2 = 10 * (sensor.readTemperature());

int decimalPart    = temp2 % 10 ;
int unitsPart       = (temp2/10) % 10 ;
int tensPart        = (temp2/100) % 10 ;
int hundredsPart  = (temp2/1000) % 10 ;
``````

If you want to represent negative centigrage temperatures as well, however, you probably don't want each digit to be negative so you'd have to set a flag to indicate a negative temperature and force temp2 to always be positive.

work great with this code

``````  temp = 10*sensor.readTemperature();

dezenaTemp = temp / 100;