Evaluate math from string

If I have the string “12 * 12”, how can I evaluate the expression and return the resulting 144 as an integer. The string could ultimately be any valid math equation. I’m not concerned with validating the expression, just returning the result.

1 Like

I found it. Looks like Code.eval_string/3 does the trick. I’m aware of the risk in this approach, but since this is an internal app used by a select few people, I’m ok with it. If there’s a less risky way to do it, I would love to know.

1 Like

this one maybe… though I can’t speak of how much more secure it is… as I don’t use it…

3 Likes

Thanks for the link @outlog! This definitely looks better.