DEV Community

Barret Blake
Barret Blake

Posted on • Originally published at barretblake.dev on

Function Friday – Data Type Conversion Functions – Bool, Decimal, Float, Int, String

For this batch of functions, we’ll start delving into the data type conversion functions. For the first group let’s look at the base type conversions. They all work pretty much the same way.

Bool

The bool function is used to convert input data into boolean form (i.e. true or false). The format is pretty straightforward:

bool(<value>)
Enter fullscreen mode Exit fullscreen mode

The value can be of number or string data types. The output depends on the data type of the input. Numbers all work the same way, whether it’s an int, float, or decimal. If the input number is 0, bool returns false. If the input number is any other value, positive or negative, and the result is true.

For strings, the output depends on the text. Literal values of ‘true’ and ‘false’ passed in return boolean values of true and false respectively. Any other string value will throw an error message as that data cannot be converted to a boolean. The same holds true for complex variable types like arrays. An array cannot be converted into a boolean and will throw an error message. And lastly, any variable that holds a null value, regardless of type, will also throw an error message.

Examples:

bool('true') //returns true
bool(1.4435) //returns true
bool(0.0) //returns false
bool(null) //throws an error
bool('hello') //throws an error
bool([1,2,3]) //throws an error
Enter fullscreen mode Exit fullscreen mode

Decimal & Float

The decimal and float functions work the same way. They will return any number or any string which can be parsed as a number, as a number of that type. The format is the same as the others:

decimal(<value>)
float(<value>)
Enter fullscreen mode Exit fullscreen mode

The value must be something that can be interpreted as a number. For other number types, the conversion is simple. For strings, the string must contain a number and only a number. For example: “1.24442” is a value string value, while “Hello, 1.24442” is not. Similarly, if the number contains a symbol, such as a currency symbol, will not work and will throw an error.

Examples:

decimal(123) //returns 123 (as a decimal)
float('123.45') //returns 123.45
decimal('$123.45') //throws an error
Enter fullscreen mode Exit fullscreen mode

Int

The int function works slightly differently than decimal and float. The pattern is the same:

int(<value>)
Enter fullscreen mode Exit fullscreen mode

For int, the input must be convertible to a number without a decimal point. If the input has a decimal point value, it will throw an error as an invalid value. Thus, “123” is a value input, while “123.45” is not.

Examples:

int('123331') //returns 123331
int('123.331') //returns an error
Enter fullscreen mode Exit fullscreen mode

String

The string function is straightforward. It converts the input into a string version. It works with any input data type, taking whatever it is and returning a single string of characters with its value. The format is the same:

string(<value>)
Enter fullscreen mode Exit fullscreen mode

For a number, it simply returns the number as a string. For a JSON variable, it returns the JSON content as a single string with escaped quotation marks around the text values. For an array, it returns a text representation of the array. For a string it returns…. well… a string.

Examples:

string(123.45) //returns "123.45"
string({"id":"G134234"}) //returns "{\\"name\\":\\"G134234\\"}"
string([1,2,3]) //returns "[1,2,3]"
Enter fullscreen mode Exit fullscreen mode

Conclusion

The first batch of conversion functions are all pretty straightforward. Next time we’ll delve into some of the more complex conversion functions.

The post Function Friday – Data Type Conversion Functions – Bool, Decimal, Float, Int, String first appeared on Barret Codes.

Top comments (0)