General area when it fits no where else

Moderator: Mmiscool

User avatar
By nedudgi
#65883
heckler wrote: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

Add 0.05, multiply by 100, truncate to integer, and divide by 10. The addition of 0.05 is for rounding 0.85 to 0.9.
User avatar
By heckler
#65885 @electroguard
"round to the nearest whole integer"
does the RND() function do that?? if so the doc's are not very clear on how to use it.
round(x):
Return the integral value nearest to x rounding halfway cases away from zero, regardless of the current rounding direction.


@nedudgi
"add 0.05"
I want the rounding to be part of the code not manual.

Thanks for both your feedback
dwight

I solved it in a round about way (see next post)
User avatar
By heckler
#65886 Ok, here is what I came up with, and it is working perfectly.

Code: Select all[GetTemp]
oledcls

curr = temp(0)
oledprint curr,0,6 'show the raw Celcius value for refrence Ln6
tf = curr * 9
tf = tf / 5
tf = tf + 32
oledprint tf,0,0   'show the raw F value Ln0

tint = int(tf)
oledprint tint,0,1  'truncate and show the whole int value Ln1

trnd = tf * 100
trnd = trnd % 10
oledprint trnd,0,2  'value used to determine rounding Ln2

tdec = tf * 10
tdec = tdec % 100
tdec = tdec % 10    'this is the 1 digit value after the decimal pt
if trnd > 4 then    'test if rounding up is needed
 tdec = tdec + 1    'add 1 to round up
endif
trnd = 0            'clear the trnd value
tdec = tdec / 10
oledprint tdec,0,3  'rounded value Ln3

tfinal = tint + tdec 'combine the whole int and the decimal part
oledprint tfinal,0,4  'show final rounded temp Ln4

wait


some comments...
not sure why, but I had to obtain the decimal remainder in two steps
Code: Select alltdec = tf * 10
tdec = tdec % 100
tdec = tdec % 10    'this is the 1 digit value after the decimal pt

I tried using tdec % 1000 but it does not work

I also found that I had to clear the trnd value used to test for rounding. This is because sometimes the raw value does not contain a lot of digits after the decimal. I believe this is normal as sometimes the temp is exactly, say, 75 degrees (with no digits after the dicimal) and the conversion is expecting to always have digits after the decimal.

This bit of code is working exactly the way I wanted it to.
I'd love for someone to make it more compact :mrgreen: but it is working.
I have tested several different values and mathematically it is spot on.

thoughts??
dwight

PS. here is the complete (messy, not final) working code if anyone wants to test it.
you may need to change the oledprint to wprint or print depending on what you have at hand.
Code: Select allmemclear
cls
counter = 0
wifiName = read(WIFIname)
wifiPass = read(WIFIpass)
time.setup(-7,0)
wprint "Freezer Alarm<br><br>"
textbox wifiName
cssid htmlid(),"position: absolute; left: 110px; display:block;width:110px"
wprint "WiFi Name:"
wprint "<br>"
textbox wifiPass
cssid htmlid(),"position: absolute; left: 110px; display:block;width:110px"
wprint "WiFi Pass:"
wprint "<br>"
button "Store WIFI Settings", [wifi_update]
'
wprint "<br><br>Enter HIGH Temp Alarm setpoint<br>"
textbox alarm
cssid htmlid(),"position: absolute; left: 110px; display:block;width:110px"
wprint "Alarm deg F"
wprint "<br>"
button GetTemp,[GetTemp]
'timer 1000, [GetTemp]
wait

[GetTemp]
oledcls
counter = counter + 1
if counter = 60 then gosub [GetTime]
curr = temp(0)
oledprint curr,0,6
tf = curr * 9
tf = tf / 5
tf = tf + 32
oledprint tf,0,0

tint = int(tf)
oledprint tint,0,1

trnd = tf * 100
trnd = trnd % 10
oledprint trnd,0,2

tdec = tf * 10
tdec = tdec % 100
tdec = tdec % 10
if trnd > 4 then
 tdec = tdec + 1
endif
trnd = 0
tdec = tdec / 10
oledprint tdec,0,3

tfinal = tint + tdec
oledprint tfinal,0,4

oledprint ip(),0,7
wait
'
[GetTime]
counter = 0
bla = time()
dw = mid(bla,1,3) 'dow
mh = mid(bla,5,3) 'month
dt = mid(bla,9,2) 'date
hh = mid(bla,12,2) 'hour
mm = mid(bla,15,2) 'min
ss = mid(bla,18,2) 'sec
yr = mid(bla,21,4) 'year
oledprint dw,8,0
oledprint mh,8,1
oledprint dt,12,1
oledprint yr,8,2
oledprint hh,8,4
oledprint ":",10,4
oledprint mm,11,4
return
'
[wifi_update]
write(WIFIname,wifiName)
write(WIFIpass,wifiPass)
returngui
cls
print "REBOOTING..."
print "Clock will attempt to connect to new network settings..."
delay 3000
reboot
wait
'