There is data from the temperature sensor (arduino + ds18b20 + dallas temp library ),

float temp = 26.23; 

It is necessary to write into the variable only the integer part , that is, 26 , I can output: Serial.println (temp,0); but I cannot write to a variable.

  • And you just need to cut off the fractional part, and not round it to the whole? If so, you can ask why? - 4per
  • one
    @ 4per Precisely cut off the fractional part, since the temperature (in the fractional part) changes very often and for comparison with the preceding one I’d better use the integer part - mocart
  • What is the problem prikastit? so (int)temp - ishidex2

4 answers 4

Float is immediately almost 1kb of space in the sketch + 4 more bytes for each variable. Do not be so ... The usual operation * 100 when receiving data and /% 100 when displaying them on the screen solves your problem

    Made a trace. using cast : introduced an additional variable int inttemp; then converted the temp variable with the float value ( 26.23 ) to integer inttemp = (int)temp; and got 26 on the way out

    • Bad advice. Why keep some temporary variables? There and so places with gulkin nose! - Vadim Teselkin
    • the easiest option is michael_best

    Issuance google http://arduino.ru/forum/programmirovanie/rabota-s-float

    You can do the "foot" conversion as follows:

     float v; long V; ... v=13.67; V=round(v*10); V/10 // целая часть числа V%10 // дробная часть числа (1 позиция) 

    And even easier just to bring to the float. in avr there is no float as such and in 99% of cases it is possible to do without it.

    • What does it mean to not bring to float? - mocart
    • don't use it at all, I suppose - 4per

    I would do like this

     int temp = (int)26.23; Serial.println (temp,0);