-->
Page 1 of 2

How to round numeric value and limit number of digits??

PostPosted: Fri May 12, 2017 10:59 pm
by heckler
Hey Group,

I am reading a ds18b20 temperature sensor.
The value returned is generally a number with 4 digits after the decimal point.
ie. 86.0625
I would like to round it to just 1 digit after the decimal but I would like to round properly such that 86.0625 will display as 86.1

I would greatly appreciate any guidance on how to do this in espBASIC

thanks in advance
dwight

Re: How to round numeric value and limit number of digits??

PostPosted: Sat May 13, 2017 12:00 am
by jimgiordano
my first guess would be

(round(num*10.))/10.

Re: How to round numeric value and limit number of digits??

PostPosted: Sat May 13, 2017 3:52 am
by taikapanu
Hi,

Not familiar with BASIC but here is what I found from documentation:
https://docs.google.com/document/d/1EiY ... YyJon8/pub
round(x):

Return the integral value nearest to x rounding halfway cases away from zero, regardless of the current rounding direction.


So maybe first multiply by 10 to move desimal --> use round function --> divide by 10 to get that one desimal back.

No idea if it works but something to try :)

Re: How to round numeric value and limit number of digits??

PostPosted: Sat May 13, 2017 3:55 am
by Electroguard
Times the data by 10 (for 1 decimal place)
Round it to nearest whole integer
Divide by 10

It works fine