title | date |
---|---|
Number formulas |
2017-03-30 05:00:00 UTC |
In Ruby, Fixnum refers to integers, e.g. 9, 10, 11, while Float refers to decimals, e.g. 1.75.
Workato supports a variety of number formulas. Formulas in Workato are whitelisted Ruby methods, and therefore not all Ruby methods are supported. You can always reach out to us to add additional formulas to the whitelist. Syntax and functionality for these formulas are generally unchanged. Take note that most formulas will return an error and stop the job if it tries to operate on nulls (expressed as nil
in Ruby), except for present?
, presence
and blank?
.
You can refer to the complete Ruby documentation for Fixnum (integers) here as well as the Ruby documentation for Float (decimals) here.
In the cases of arithmetic operations, whether the values are of integer types or decimal (float) types are important. Formulas will alway stick to the types given as input, and the returned result will be of the most precise type.
For example:
- If integer values are provided, an integer value will be returned.
- If float values are provided, a float value will be returned.
- If both float and integer values are provided, a float value will be returned, as that is more precise.
This operator allows the addition of operands on either side.
Example | Result | Type |
---|---|---|
4 + 7 |
11 | Fixnum |
4.0 + 7 |
11.0 | Float |
4.0 + 7.0 |
11.0 | Float |
This operator subtracts the right hand operand from the left hand operand.
Example | Result | Type |
---|---|---|
4 - 7 |
-3 | Fixnum |
4.0 - 7 |
-3.0 | Float |
4.0 - 7.0 |
-3.0 | Float |
This operator multiplies the operands on either side.
Example | Result | Type |
---|---|---|
4 * 7 |
28 | Fixnum |
4.0 * 7 |
28.0 | Float |
4.0 * 7.0 |
28.0 | Float |
Divides left hand operand by right hand operand.
Example | Result | Type |
---|---|---|
4 / 7 |
0 | Fixnum |
4.0 / 7 |
0.571428... | Float |
7 / 4 |
1 | Fixnum |
7 / 4.0 |
1.75 | Float |
7.0 / 4 |
1.75 | Float |
7.0 / 4.0 |
1.75 | Float |
Divides left hand operand by right hand operand and returns the remainder.
Example | Result | Type |
---|---|---|
4 % 7 |
4 | Fixnum |
4.0 % 7 |
4.0 | Float |
4 % 7.0 |
4.0 | Float |
7 % 4 |
3 | Fixnum |
7.0 % 4.0 |
3.0 | Float |
This function returns the absolute (positive) value of a number.
Example | Result | Type |
---|---|---|
-45.abs |
45 | Fixnum |
45.abs |
45 | Fixnum |
-45.0.abs |
45.0 | Float |
-45.67.abs |
45.67 | Float |
Rounds off a numerical value to a specified after the formula. Accepts negative values.
Example | Result | Type |
---|---|---|
1234.567.round |
1235 | Fixnum |
1234.567.round(2) |
1234.57 | Float |
1234.567.round(-2) |
1200 | Fixnum |
This function checks the input and returns true if it is a null or an empty string.
Example | Result |
---|---|
" ".blank? |
true |
null.blank? |
true |
-45.blank? |
false |
0.blank? |
false |
This function will check the input number, returning its value if there is one present, else returning nil.
Example | Result | Type |
---|---|---|
" ".presence |
nil | nil |
null.presence |
nil | nil |
-45.presence |
-45 | Fixnum |
0.presence |
0 | Fixnum |
45.0.presence |
45.0 | Float |
-45.0.presence |
-45.0 | Float |
This function will check the input, returning true if there is a value present. If input is null or an empty string, formula returns false.
Example | Result |
---|---|
" ".present? |
false |
null.present? |
false |
-45.present? |
true |
0.present? |
true |
45.0.present? |
true |
This function can be used on a number to only take its integer value. Floats will be rounded down. To round to closest integer, use the round
formula instead.
Example | Result | Type |
---|---|---|
45.67.to_i |
45 | Fixnum |
-45.67.to_i |
-45 | Fixnum |
45.to_i |
45 | Fixnum |
-45.to_i |
-45 | Fixnum |
0.to_i |
0 | Fixnum |
This function can be used on a number to retain its decimal values. When applied to an integer, it converts the data type from fixnum to float.
Example | Result | Type |
---|---|---|
45.67.to_f |
45.67 | Float |
-45.67.to_f |
-45.67 | Float |
45.to_f |
45.0 | Float |
-45.to_f |
-45.0 | Float |
0.to_f |
0 | Float |
This function converts a number into a string.
Example | Result | Type |
---|---|---|
45.67.to_s |
"45.67" | String |
-45.67.to_s |
"-45.67" | String |
45.to_s |
"45" | String |
-45.to_s |
"-45" | String |
0.to_s |
"0" | String |
This function converts a number into a currency string. The number of decimal places to round to can be defined with an additional parameter.
By adding a parameter with a locale and country code, it will add the appropriate currency symbol.
Example | Result | Type |
---|---|---|
45.678.to_currency |
$45.68 | Currency |
45.6789.to_currency(precision:3) |
$45.678 | Currency |
45.67.to_currency(locale: fr) |
45.67 € | Currency |
This function converts a number into a formatted phone number. By default, the delimiter is a dash (-), but delimiters can be specified via a parameter.
By adding a parameter with area code: true, the first three numbers will be enclosed in brackets, representing the area.
By adding a parameter with extension: number, the formatted phone number will have the extension appended at the end.
By adding a parameter with country_code: number, the formatted phone number will be prefixed with the country code.
Example | Result |
---|---|
1112223333.to_phone |
"111-222-3333" |
1112223333.to_phone(delimiter: " ") |
"111 222 3333" |
1112223333.to_phone(area_code: true) |
"(111) 222-3333" |
1112223333.to_phone(country_code: 1) |
"+1-111-222-3333" |
1112223333.to_phone(area_code: true, country_code: 1) |
"+1-(111) 222-3333" |
1112223333.to_phone(extension: 444) |
"111-222-3333 x 444" |
To convert a value of other data types, e.g. string, date, into an integer or a float, use the to_i and the to_f formulas respectively.