Temps rounded to whole numbers?



  • Trying to get the DS18B20 to round to a whole number no decimal point. I'm using this and getting the temps down to .1 decimal point.

    // Fetch and round temperature to one decimal
    		float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric ? sensors.getTempCByIndex(i) : sensors.getTempFByIndex(i)) * 10.)) / 10.;
    

    What do I do next to get a whole number only, I don't like all the minor changes flooding my controller.

    complete node code is below.

    https://codebender.cc/sketch:238531


  • Mod

    @DrJeff said:

    float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric ? sensors.getTempCByIndex(i) : sensors.getTempFByIndex(i)) * 10.)) / 10.;

    Either store the temperature in an int (and round to nearest integer by adding 0.5 to the temperature):

      int temperature = static_cast<int>(0.5+(gw.getConfig().isMetric ? sensors.getTempCByIndex(i) : sensors.getTempFByIndex(i)));
    

    Or keep it in a float, rounded to the nearest integer:

      float temperature = round(gw.getConfig().isMetric ? sensors.getTempCByIndex(i) : sensors.getTempFByIndex(i)));
    


  • @Yveaux said:

    float temperature = round(gw.getConfig().isMetric ? sensors.getTempCByIndex(i) : sensors.getTempFByIndex(i)));

    I will try this Thanks for the help.


Log in to reply
 

Suggested Topics

  • 3
  • 24
  • 2
  • 2
  • 3
  • 2

1
Online

11.2k
Users

11.1k
Topics

112.5k
Posts