Number formulas
In Ruby, Fixnum refers to integers, e.g. 9, 10, 11, while Float refers to decimals, e.g. 1.75.
AppConnect supports a variety of number formulas. Formulas in AppConnect 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?.
Arithmetic operations
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.
The add (+) operator
This operator allows the addition of operands on either side. This section talks about number arithmetics. Date arithmetics is possible as well.
Sample Usage
Formula | Result | Type |
---|---|---|
4 + 7 | 11 | Fixnum |
4.0 + 7 | 11.0 | Float |
4.0 + 7.0 | 11.0 | Float |
The subtract (-) operator
This operator subtracts the right hand operand from the left hand operand. This section talks about number arithmetics. Date arithmetics is possible as well.
Sample Usage
Formula | Result | Type |
---|---|---|
4 - 7 | -3 | Fixnum |
4.0 - 7 | -3.0 | Float |
4.0 - 7.0 | -3.0 | Float |
The multiply (*) operator
This operator multiplies the operands on either side.
Sample Usage
Formula | Result | Type |
---|---|---|
4 * 7 | 28 | Fixnum |
4.0 * 7 | 28.0 | Float |
4.0 * 7.0 | 28.0 | Float |
The divide (/) operator
Divides left hand operand by right hand operand.
Sample Usage
Formula | 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 |
The exponential (**) operator
Left hand operand to the power of the right hand operand.
Sample Usage
Formula | Result | Type |
---|---|---|
5**3 | 125 | Fixnum |
4**1.5 | 8.0 | Float |
4.0**2 | 16.0 | Float |
3**-1 | "1/3" | Rational |
8**(3**-1) | 2.0 | Float |
7**-1.6 | 0.044447... | Float |
The modulo (%) operator
Divides left hand operand by right hand operand and returns the remainder.
Sample Usage
Formula | 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 |
Other number formulas
abs
Returns the absolute (positive) value of a number.
Syntax
number.abs
number - An input integer or float.
Sample usage
Formula | Result |
---|---|
45.abs | 45 |
-45.abs | 45 |
45.67.abs | 45.67 |
-45.67.abs | 45.67 |
round
Rounds off a numerical value. This formula returns a value with a specified number of decimal places.
Syntax
number.round(offset)
number - An input integer or float.
offset - (optional) The number of decimal places to return, you can provide negative values. If not specified, this formula will return the number with no decimal places.
Sample usage
Formula | Result |
---|---|
1234.567.round | 1234 |
1234.567.round(2) | 1234.56 |
1234.567.round(-2) | 1200 |
Conditionals
blank?
This formula checks the input and returns true if it is non a value number or if it is null.
Syntax
Input.blank?
Input - An input datapill. It can be a string, number, date, or datetime datatype.
Sample usage
Formula | Result |
---|---|
123.blank? | false |
0.blank? | false |
nil.blank? | true |
"".blank? | true |
How it works
If the input is null or an empty string, the formula will return false. For any other data, it returns true.
even?
Checks the integer input and returns true if it is an even number.
Syntax
integer.even?
integer - An input integer.
Sample usage
Formula | Result |
---|---|
123.even? | false |
1234.even? | true |
odd?
Checks the integer input and returns true if it is an odd number.
Syntax
integer.odd?
integer - An input integer.
Sample usage
Formula | Result |
---|---|
123.odd? | true |
1234.odd? | false |
is_not_true?
Converts a value to boolean and returns true if the value is not truthy.
Syntax
Input.is_not_true?
Input - An input number or a string.
Sample usage
Formula | Result |
---|---|
123.is_not_true? | false |
"false".is_not_true? | false |
0.is_not_true? | true |
"".is_not_true? | false |
nil.is_not_true? | true |
How it works
Converts the input into a boolean and returns true if the value is not truthy.
truthy vs falsey
false, nil, 0 and empty strings ("") are falsey, all other numbers and string values are truthy.
is_true?
Converts a value to boolean and returns true if the value is truthy.
Syntax
Input.is_true?
Input - An input number or a string.
Sample usage
Formula | Result |
---|---|
123.is_true? | true |
"false".is_true? | true |
0.is_true? | false |
"".is_true? | false |
nil.is_true? | false |
How it works
Converts the input into a boolean and returns true if the value is truthy.
truthy vs falsey
false, nil, 0 and empty strings ("") are falsey, all other numbers and string values are truthy.
present?
This formula will check the input and if there is a value present, it will return true. If the input is nil, an empty string or an empty list, the function will return false.
Syntax
Input.present?
Input - An input datapill. It can be a string, number, date, or list datatype.
Sample usage
Formula | Result |
---|---|
"Any Value".present? | true |
123.present? | true |
0.present? | true |
"2017-04-02T12:30:00.000000-07:00".present? | true |
nil.present? | false |
"".present? | false |
[].present? | false |
How it works
If the input is null, an empty string or an empty list, the formula will return false. For any other data, it returns true.
Evaluating a list with nil values
Only an empty list will return false.
[].present? returns false.
A list with nil and empty string will return true.
[nil,""].present? returns true.
presence
Returns the data if it exists, returns nil if it does not.
Syntax
Input.presence
Input - An input datapill. It can be a string, number, date, or datetime datatype.
Sample usage
Formula | Result |
---|---|
nil.presence | nil |
"".presence | nil |
"Any Value".presence | "Any Value" |
45.0.presence | 45.0 |
0.presence | 0 |
How it works
If the input is null or an empty string, the formula will return nil. For any other data, it returns the orignal input data.
Conversions
ceil
Rounds the input number to the next greater integer or float. You can specify the precision of the decimal digits.
Syntax
number.ceil(precision)
number - An input integer or float.
precision - (optional) The number of decimal places to return, you can provide negative values. If not specified, this formula will return the number with no decimal places.
Sample usage
Formula | Result |
---|---|
1234.567.ceil | 1235 |
-1234.567.ceil | -1234 |
1234.567.ceil(2) | 1234.57 |
1234.567.ceil(-2) | 1300 |
floor
Rounds the input number to the next smaller integer or float. You can specify the precision of the decimal digits.
Syntax
number.floor(precision)
number - An input integer or float.
precision - (optional) The number of decimal places to return, you can provide negative values. If not specified, this formula will return the number with no decimal places.
Sample usage
Formula | Result |
---|---|
1234.567.floor | 1234 |
-1234.567.floor | -1235 |
1234.567.floor(2) | 1234.56 |
1234.567.floor(-2) | 1200 |
to_f
Converts data to a float (number) datatype.
Syntax
Input.to_f
Input - An number input data. You can use a string datatype or a integer datatype.
Sample usage
Formula | Result |
---|---|
45.to_f | 45.0 |
-45.to_f | -45.0 |
"45.67".to_f | 45.67 |
"AppConnect".to_f | 0 |
How it works
This formula checks whether the input contains any numbers, if no numbers are found, it returns 0. If the number does not have a decimal point, .0 will be added the number.
to_i
Converts data to an integer (whole number) datatype.
Syntax
Input.to_i
Input - An number input data. You can use a string datatype or a float datatype.
Sample usage
Formula | Result |
---|---|
45.43.to_i | 45 |
-45.43.to_i | -45 |
"123".to_i | 123 |
"AppConnect".to_i | 0 |
How it works
This formula checks whether the input contains any numbers, if no numbers are found, it returns 0. If the number has a decimal point, everything after the decimal will be omitted.
Check for integers
You can use this formula to check if a string contains an integer. If the input does not contain any numbers, the formula will return 0.
to_s
Converts data to a string (text) datatype.
Syntax
Input.to_s
Input - Any input data. You can use number, array, object, or datetime datatypes.
Sample usage
Formula | Result |
---|---|
-45.67.to_s | "-45.67" |
"123".to_s | "123" |
[1,2,3].to_s | "[1,2,3]" |
{key: "AppConnect"}.to_s | "{:key=>"AppConnect"}"" |
"2020-06-05T17:13:27.000000-07:00".to_s | "2020-06-05T17:13:27.000000-07:00" |
"2020-06-05T17:13:27.000000-07:00".to_s(:short) | "05 Jun 17:13" |
"2020-06-05T17:13:27.000000-07:00".to_s(:long) | "June 05, 2020 17:13" |
How it works
This formula returns a string representation of the input data.
Quicktip: Output is a string datatype.
A string representation of a list cannot be used in a repeat step.
to_currency
Formats integers/numbers to a currency-style.
Syntax
Input.to_currency
Input - Any input string.
Sample usage
Formula | Description | Result |
---|---|---|
"345.60".to_currency | Adds default currency symbol "$" | "$345.60" |
Advance sample usage
Learn more about advance usage for the to_currency formula. A comma-separated combination of these may be used to achieve the desired currency format. For example:
1"12345.678".to_currency(delimiter: ".", format: "%n %u", precision: 2, separator: ",", unit: "€")2
Formula | Description | Result |
---|---|---|
"345.60".to_currency(unit: "€") | Changes the default currency unit | "€345.60" |
"345.60".to_currency(format: "%n %u") | Changes the position of the number relative to the unit (where the number is represented by %n and the currency unit is represented by %u). Accepts 0 or 1 spaces in between. Defaults to "%u%n". | "345.60 $" |
"-345.60".to_currency(negative_format: "(%u%n)") | Specifies the format when the number is negative (where the number is represented by %n and the currency unit is represented by %u). | "($345.60)" |
"345.678".to_currency | Precision defaults to 2 decimal places | "$345.68" |
"345.678".to_currency(precision: 3) | Change the precision by specifying the number of decimal places | "$345.678" |
"345.678".to_currency(separator: ",") | Specify the decimal separator as ".", "," or " ". Defaults to ".". | "$345,68" |
"12345.678".to_currency(delimiter: ".") | Specify the thousands separator as ",", "." or " ". Defaults to ",". | ""$12.345.68" |
to_phone
Converts string or number to a formatted phone number (user-defined).
Syntax
Input.to_phone
Input - Any input string or number.
Sample usage
Formula | Result |
---|---|
"5551234".to_phone | 555-1234 |
1235551234.to_phone | 123-555-1234 |
1235551234.to_phone(area_code: true) | (123) 555-1234 |
1235551234.to_phone(delimiter: " ") | 123 555 1234 |
1235551234.to_phone(area_code: true, extension: 555) | (123) 555-1234 x 555 |
1235551234.to_phone(country_code: 1) | +1-123-555-1234 |
"123a456".to_phone | 123a456 |