Converting Data Between Types
Most of the time, you don’t have to declare types in Crystal, which can be a relief. Sometimes, though, you’ll need to make certain that you’re working with values of a certain type. You shouldn’t count much on Ruby-style automatic conversions: Crystal isn’t only strongly typed—it wants you to be very careful with conversions. Type conversion is something you’ll need to do explicitly in order to satisfy the Crystal compiler. (Though Crystal will deal with the simplest conversions, such as int8 to int32 automatically.)
Money provides a good example: currency rates are numbers with many digits after the decimal point, as in 1 USD is worth 64.34603985 Indian Rupees (INR) today. Although banks don’t use floating-point ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access