3

My arduino is meisuring the tempature and gives values like: 28.58 Degrees Celcius. I would like to round this up to one decimal. I could make the variable one shorter by cutting the "8" off which would desplay 28.5. Yet this feels a bit like cheating as it mathimatical not correct. Does anyone know how to round up/down. Which would show my tempature as 28.6.

Extra advice needed. I am sending the tempature by Serial to my RPi which has a python code running. Would you suggest doing the rounding in the Arduino code or in The Python code.

Thanks in Advance!

4
  • Wouldn't it be better to round to nearest rather than systematically rounding up. Commented Jun 20, 2017 at 9:29
  • Yea I would love to able to round it the mathematical way. 28.65 <, will become 28.6 and 28.65 ≥, will become 28.7 Commented Jun 20, 2017 at 10:36
  • Then let Serial.print() do the rounding, as per @Jot's answer. Commented Jun 20, 2017 at 11:22
  • Personally, I would not round until the data is on the Pi. Why lose precision when you don't need to? What if, at some later point, you decide you need to do more calculations on the data on the Pi? Python has a round() function built in.
    – 001
    Commented Jun 20, 2017 at 12:06

2 Answers 2

4

As far as I know, the Serial.print (or println) does rounding up and down. All you have to do is take a float variable and do Serial.print( value, 1);

Without extra parameter, the default is choosen, which is 2 decimal digits: Serial.print( value, 2);

Why do you want to shorten the bytes over the Serial ? You can just as well send two decimal digits and process the value in Python.

3
  • Thats what the last part of my question is about ;) wondering where to process, in python on my Rpi or before sending. Commented Jun 20, 2017 at 10:20
  • 1
    Any rounding should be done as close to the final output as possible. If your serial port is fast enough then send 2 or 3 digits and round when the Pi is outputting the data. You don't want to base calculations on rounded values if you can help it, that introduces the possibility of errors. That said it's a complete waste of time sending random numbers, if the temperature sensor has an error of +/- 1 degree then the first decimal place may be useful, anything after that is meaningless.
    – Andrew
    Commented Jun 20, 2017 at 14:15
  • Okay agreed! It more precise to process at the final stage, yet the accuracy of the sensor is not there, so I will save bandwidth by using Serial.print(value, 1); Commented Jun 20, 2017 at 15:00
4

You can round in C by multiplying for the significance, adding +0.5, round down (equals as casting to an integer) and divide.

float f_rounded = ((int) (f * 10.0 + 0.5) / 10.0);

28.6 will be:

float f_rounded = ((int) (28.6 * 10.0 + 0.5) / 10.0) 
                = ((int) 286.5) / 10.0 = 286 / 10.0 = 28.6

In Python it is equal (except the cast operator is int(...)

Update: for negative numbers +0.5 should be -0.5

Credits for Edgar Bonet: using round works for both positive and negative numbers:

float f_rounded = round(f * 10) / 10.0;
3
  • 1
    This would round -28.58 to -28.5.
    – 001
    Commented Jun 20, 2017 at 12:04
  • @JohnnyMopp Updated my answers, thanks for the comment Commented Jun 20, 2017 at 12:29
  • 1
    Why not just float f_rounded = round(f * 10) / 10.0;? This handles both the positive and negative cases. Commented Jun 20, 2017 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.