#how can i check if i'm getting an int or a float?
1 messages · Page 1 of 1 (latest)
Hey! Where are you taking your input from?
i'm working through an exercism. pence_to_pounds.
just curious, as it wasn't clear in the tests what i'd be expecting.
which got me wondering how i'd handle that in the function itself
Gleam is statically typed, so you can't accept just a 'number' like you might in JS. Are you working in the Exercism editor or locally?
interesting, usually they do fully type the functions they provide you
that one is given a float and expected to return a float
oh sorry, they give you an int and expect a float back
yeah thanks. I got it working, but was reading the doc to understand if i could type check and cast if required
it's int he description that pence is an integer number of pence, but it's a shame they forgot to add the types into the code
but since it's static. i'm assuming it's not really a thing that's needed
the compiler will tell you with an error if you have the wrong types
yeah, the inputs always have a definite type even if it's not explicit
and as far as i'm aware so far, gleam doesn't have union types, so it'd be impossible for the input to be eg. Float | Int
awesome thanks.
while i have everyone.
is there a nicer way to write this.
pub fn pounds_to_string(pounds) {
"£"
|> string.append(float.to_string(pounds))
}
Yeah if you want to go from a float to an int or vice versa you'll have to use int.to_float or float.truncate/float.round, there's no implicit casts
Yeah, you can use string concatenation directly:
pub fn pounds_to_string(pounds: Float) -> String {
"£" <> float.to_string(pounds)
}
pub fn pence_to_pounds(pence) {
int.to_float(pence) /. 100.0
}
pub fn pounds_to_string(pounds) {
"£" <> float.to_string(pounds)
}
ah, Jak beat me to it
this was my solution for it
lovely! I knew there'd be a nicer way.
Thanks team!
You're welcome! Keep the questions coming if you need help 
check other peoples solutions after submitting your own, you can often find some nicer ways to do things easily that way
great tip. thanks