Main Site Documentation

DS18B20 Temperature Sensor, adjusting precision of temperature


I’m using the code found in this example to get started with my first project using the DS18B20. I have a working example and the temperatures returned actually match the wall thermometers in my office. Not bad for a first attempt :slight_smile:

In many example I’ve seen (of course I can’t find any right now), the temperature precision can be output to 1 or 2 decimal places.For example, the above code currently outputs the temperature as a whole number (18) and I want to show the temperature to one (18.2) or two (18.24) decimal places. How do I do this ?



Well, first of all, that temp sensor only has 0.5C accuracy, so going to a second decimal place is meaningless, and gives the end user (you?) a false sense of precision. I believe that’s why the unmodified driver only gives you an integer. If the hardware error is plus or minus 1/2 a degree, even using one decimal place is dubious.

You can convert the double (or maybe it’s a float) to the desired number of decimal places using ToString(“N1” ) for display, like in the following link .