xexpanderx / Conky-themes

Conky themes
53 stars 18 forks source link

error, bad argument #2 to 'format' (number expected, got string) #2

Closed AlexP11223 closed 5 years ago

AlexP11223 commented 5 years ago

I get error

conky: llua_do_call: function conky_start_widgets execution failed: settings.lua:93: bad argument #2 to 'format' (number expected, got string)

here

temperature = tostring(tonumber(string.format("%.0f", temperature)))

This works for me

./openweather.py --get_temp_c --api_key ................. --city Vilnius --ccode LT
16.22

I tried to remove this formatting line (output the original string) and it worked, except that it outputs 16.22 instead of 16.

Looks like some kind of decimal number parsing issue? Most likely because my locale uses , instead of ..

My locale:

locale
LANG=en_US.UTF-8
LANGUAGE=en_US:en
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=ru_RU.UTF-8
LC_TIME=ru_RU.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=ru_RU.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=ru_RU.UTF-8
LC_NAME=ru_RU.UTF-8
LC_ADDRESS=ru_RU.UTF-8
LC_TELEPHONE=ru_RU.UTF-8
LC_MEASUREMENT=ru_RU.UTF-8
LC_IDENTIFICATION=ru_RU.UTF-8
LC_ALL=

PopOS 19.04

xexpanderx commented 5 years ago

That error is spitting out initially when starting the conky due to hickups (because temperature is empty when trying to parse) from openweather - connection. The error should disappear after a couple of seconds?

AlexP11223 commented 5 years ago

No, it doesn't disappear.

It works if I replace . to , using strings.gsub before formatting. It's a bad solution obviously, the proper way to fix this is to parse the number ignoring the locale (always use .), but I don't know how to implement this in conky Lua.

xexpanderx commented 5 years ago

Alright. It sounds like a work-around for the moment;

temperature = tostring((string.format("%.0f", string.gsub(temperature,",","."))))

AlexP11223 commented 5 years ago

It replaces comma to dot before formatting?

It would not work (and the weather script already outputs with dot) because the issue is the opposite: format uses decimal separator from my system locale, which is not dot.

xexpanderx commented 5 years ago

It does work, just tested. See commit: https://github.com/xexpanderx/Conky-themes/commit/34dc9c86019c6df0053c7113fa4350afad971059

xexpanderx commented 5 years ago

Ah sorry, too fast. I am working on something else in parallel. Yes, format uses indeed the system locale...

xexpanderx commented 5 years ago

Try and insert this code and remove (string.gsub):

assert(os.setlocale("en_US.utf8", "numeric"))

This assumes you have "en_US.utf8" installed on your system. A problem arises if "en_US.utf8" is not installed on the system, which will give arise to an assert error.

xexpanderx commented 5 years ago

Try with commit: https://github.com/xexpanderx/Conky-themes/commit/a81a46114dd4414fecc5d0b60d6249c4bc0644e6

AlexP11223 commented 5 years ago

yeah, looks like it works. :)