Place to put your Basic demos and examples

Moderator: Mmiscool

User avatar
By lew247
#54886 How can I limit the number of decimal places?

For example in extracting the weather from Weather Underground

let speed = json(ret,"speed")
Then I want to serialprintln speed

BUT I want to limit it to just the speed in whole numbers and NO decimal places
User avatar
By forlotto
#54889 Curious to see your full code using json.

This may help in understanding the issue also would be a great lesson for others trying to learn the use of json and what it is capable of.


My guess is that you would use string functions to accomplish your task before printing the value.

But please do share.
User avatar
By lew247
#54894 It's probably possible to do what I've done a lot simpler, but I'm new to all this.
I wanted to send the output to another computer to display the values, hence the print going to the serial port
I also wanted to limit the number of json calls to once every 3 minutes which is the amount of free calls from Weather Underground without having to pay.

It's showing today's weather plus the next 3 day's forecast

If anyone wants an API key for weather Underground get one here free https://www.wunderground.com/weather/api/d/docs#getting_started

The only way I could see to get it to send the info on power up was repeat the whole program in a sub that is called on startup - hence it being so long winded!

I want to limit the "SPEED" output to the serial port to whole numbers and nothing like 6.96 which it's doing at this moment

Code: Select alltimesetup(1)
print time()
delay 500
print time()
[RUNPROGRAM1]
timer 3 * 60 * 1000, [RUNPROGRAM]
wait

[RUNPROGRAM]
serialflush
cls
let query = "api.openweathermap.org/data/2.5/weather?q=Denton,uk&lang=en&units=metric&appid=API KEY HERE"
let ret = wget(query)
let temp = json(ret,"main.temp")
let press = json(ret,"main.pressure")
let humid = json(ret,"main.humidity")
let speed = json(ret,"main.speed")
let dir = json(ret,"main.deg")
let icon = json(ret,"main.icon")
let desc = json(ret,"description")
delay 400
serialprintln "Today," & desc & "," & icon & "," & temp & "," & press &"," & speed & "," & dir & "," & humid
serialflush
cls
delay 1000

let query = "api.openweathermap.org/data/2.5/forecast/daily?&units=metric&q=Denton,UK&appid=API KEY HERE"
let ret = readopenweather(query,2)
let tim = json(ret,"dt")
let tim = unixtime(tim)
let icon = json(ret,"icon")
let main = json(ret,"main")
let desc = json(ret,"description")
let temp_min = json(ret,"temp.min")
let temp_max = json(ret,"temp.max")
let pres = json(ret,"pressure")
let humid = json(ret,"humidity")
let speed = json(ret,"speed")
let deg = json(ret,"deg")
let icon = json(ret,"icon")
delay 400
serialprintln "Day2," & tim & "," & desc  & "," &   icon & "," & temp_min & "," & temp_max & "," & press & "," & humid & "," & speed & "," & dir
serialflush

let query = "api.openweathermap.org/data/2.5/forecast/daily?&units=metric&q=Denton,UK&appid=API KEY HERE"
let ret = readopenweather(query,3)
let tim = json(ret,"dt")
let tim = unixtime(tim)
let icon = json(ret,"icon")
let main = json(ret,"main")
let desc = json(ret,"description")
let temp_min = json(ret,"temp.min")
let temp_max = json(ret,"temp.max")
let pres = json(ret,"pressure")
let humid = json(ret,"humidity")
let speed = json(ret,"speed")
let deg = json(ret,"deg")
let icon = json(ret,"icon")
delay 1000
serialprintln "Day3," & tim & "," & desc  & "," &   icon & "," & temp_min & "," & temp_max & "," & press & "," & humid & "," & speed & "," & dir
serialflush

let query = "api.openweathermap.org/data/2.5/forecast/daily?&units=metric&q=Denton,UK&appid=API KEY HERE"
let ret = readopenweather(query,4)
let tim = json(ret,"dt")
let tim = unixtime(tim)
let icon = json(ret,"icon")
let main = json(ret,"main")
let desc = json(ret,"description")
let temp_min = json(ret,"temp.min")
let temp_max = json(ret,"temp.max")
let pres = json(ret,"pressure")
let humid = json(ret,"humidity")
let speed = json(ret,"speed")
let deg = json(ret,"deg")
let icon = json(ret,"icon")
delay 1000
serialprintln "Day4," & tim & "," & desc  & "," &   icon & "," & temp_min & "," & temp_max & "," & press & "," & humid & "," & speed & "," & dir
serialflush
cls
wait


[RUNPROGRAM1]
serialflush
cls
let query = "api.openweathermap.org/data/2.5/weather?q=Denton,uk&lang=en&units=metric&appid=API KEY HERE"
let ret = wget(query)
let temp = json(ret,"main.temp")
let press = json(ret,"main.pressure")
let humid = json(ret,"main.humidity")
let speed = json(ret,"main.speed")
let dir = json(ret,"main.deg")
let icon = json(ret,"main.icon")
let desc = json(ret,"description")
delay 400
serialprintln "Today," & desc & "," & icon & "," & temp & "," & press &"," & speed & "," & dir & "," & humid
serialflush
cls
delay 1000

let query = "api.openweathermap.org/data/2.5/forecast/daily?&units=metric&q=Denton,UK&appid=API KEY HERE"
let ret = readopenweather(query,2)
let tim = json(ret,"dt")
let tim = unixtime(tim)
let icon = json(ret,"icon")
let main = json(ret,"main")
let desc = json(ret,"description")
let temp_min = json(ret,"temp.min")
let temp_max = json(ret,"temp.max")
let pres = json(ret,"pressure")
let humid = json(ret,"humidity")
let speed = json(ret,"speed")
let deg = json(ret,"deg")
let icon = json(ret,"icon")
delay 400
serialprintln "Day2," & tim & "," & desc  & "," &   icon & "," & temp_min & "," & temp_max & "," & press & "," & humid & "," & speed & "," & dir
serialflush

let query = "api.openweathermap.org/data/2.5/forecast/daily?&units=metric&q=Denton,UK&appid=API KEY HERE"
let ret = readopenweather(query,3)
let tim = json(ret,"dt")
let tim = unixtime(tim)
let icon = json(ret,"icon")
let main = json(ret,"main")
let desc = json(ret,"description")
let temp_min = json(ret,"temp.min")
let temp_max = json(ret,"temp.max")
let pres = json(ret,"pressure")
let humid = json(ret,"humidity")
let speed = json(ret,"speed")
let deg = json(ret,"deg")
let icon = json(ret,"icon")
delay 1000
serialprintln "Day3," & tim & "," & desc  & "," &   icon & "," & temp_min & "," & temp_max & "," & press & "," & humid & "," & speed & "," & dir
serialflush

let query = "api.openweathermap.org/data/2.5/forecast/daily?&units=metric&q=Denton,UK&appid=API KEY HERE"
let ret = readopenweather(query,4)
let tim = json(ret,"dt")
let tim = unixtime(tim)
let icon = json(ret,"icon")
let main = json(ret,"main")
let desc = json(ret,"description")
let temp_min = json(ret,"temp.min")
let temp_max = json(ret,"temp.max")
let pres = json(ret,"pressure")
let humid = json(ret,"humidity")
let speed = json(ret,"speed")
let deg = json(ret,"deg")
let icon = json(ret,"icon")
delay 1000
serialprintln "Day4," & tim & "," & desc  & "," &   icon & "," & temp_min & "," & temp_max & "," & press & "," & humid & "," & speed & "," & dir
serialflush

RETURN
User avatar
By bugs
#54896 BASIC has the int() function which gives you the whole number part but just chops off the decimal bit

speed = 6.96
speed1 = int(speed) ' gives you 6

so add 0.5 to the number first
speed1 = int(speed + 0.5) 'which should give you the rounded answer of 7