#how can i check if i'm getting an int or a float?

1 messages · Page 1 of 1 (latest)

ionic tartan
#

I'm day 1. just working through the stdlib. I can't see a way to type check if im getting an int or a float as input, to them ... do something with it?

odd grove
#

Hey! Where are you taking your input from?

ionic tartan
#

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

odd grove
#

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?

ionic tartan
#

in the online editor

#

so i couldn't see the test cases

celest glen
#

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

ionic tartan
#

yeah thanks. I got it working, but was reading the doc to understand if i could type check and cast if required

celest glen
#

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

ionic tartan
#

but since it's static. i'm assuming it's not really a thing that's needed

last siren
#

the compiler will tell you with an error if you have the wrong types

celest glen
#

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

ionic tartan
#

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))
}
winged latch
#

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

winged latch
celest glen
#
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

ionic tartan
#

lovely! I knew there'd be a nicer way.
Thanks team!

winged latch
#

You're welcome! Keep the questions coming if you need help espeon

celest glen
#

check other peoples solutions after submitting your own, you can often find some nicer ways to do things easily that way

ionic tartan
#

great tip. thanks