How to convert bool/str/float to float? #3965
-
Asked a few people, read many pages in The Book, but don’t get it: I need to be backwards compatible with a metadata variable that used to be boolean or boolean in a string, and is now a float value. In Python, that was easy, but how to do in Liquidsoap? I didn’t even find something like Python code:
Any guru there who can help me with this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
If I were you, then I would convert everything to string, because every other type has a convenient to string conversion and then parse the string. def something_to_float(value)
value_string = string.case(string(value))
possible_float =
try
float_of_string(value_string)
catch _ do
null()
end
possible_bool =
try
bool_of_string(value_string) ? 1. : 0.
catch _ do
null()
end
possible_float ?? possible_bool ?? 0.
end Testsa = true
b = false
c = "true"
d = "false"
e = "True"
f = "False"
g = "TRUE"
h = "FALSE"
i = 1.
j = -1.
k = "1."
l = "-1."
m = 1
n = -1
o = "1"
p = "-1"
def test(a)
print(a)
b = string.case(string(a))
print(b)
c =
try
float_of_string(b)
catch _ do
null()
end
d =
try
bool_of_string(b) ? 1. : 0.
catch _ do
null()
end
e = c ?? d
print(e)
print("")
end
test(a)
test(b)
test(c)
test(d)
test(e)
test(f)
test(g)
test(h)
test(i)
test(j)
test(k)
test(l)
test(m)
test(n)
test(o)
test(p) |
Beta Was this translation helpful? Give feedback.
If I were you, then I would convert everything to string, because every other type has a convenient to string conversion and then parse the string.
Tests