Number
Juttle Number module provides constants for numeric operations, and functions for number/string conversion. Refer to the Math module for other mathematical constants and functions.
Number constants
Juttle supports these Number constants as well as Math constants. They can be referenced without an import directive in a Juttle program, but are more commonly found in the output of certain mathematical operations than used explicitly.
Constant | Description |
---|---|
Number.MAX_VALUE | Largest positive numeric value, approximately 1.7976931348623157e+308 Larger values are converted to Infinity |
Number.MIN_VALUE | Smallest positive numeric value (closest to zero), approximately 5e-324 Smaller values are converted to 0 Note that the most negative number is -Number.MAX_VALUE |
Number.NaN | Not-A-Number value, returned from illegal mathematical operations; always compares unequal to any other value including NaN The floating point literal NaN can be used for shorter notation. |
Number.NEGATIVE_INFINITY | -Infinity value which is less than -Number.MAX_VALUE The floating point literal -Infinity can be used for shorter notation. |
Number.POSITIVE_INFINITY | Infinity value which is greater than Number.MAX_VALUE The floating point literal Infinitiy can be used for shorter notation. |
Example
const pos_inf = Number.POSITIVE_INFINITY;
emit
| put div_by_zero = 1 / 0, inf = pos_inf
| filter div_by_zero = pos_inf
| filter inf > Number.MAX_VALUE
| put nan = inf / inf
Number.fromString()
Convert a value of type String to type Number.
Number.fromString(string)
Example: Parse out HTTP status code from apache access log line, and convert to a number for mathematical comparison
emit -limit 1
| put message = '127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 404 347'
| put http_status = Number.fromString(String.split(message, ' ')[8])
| filter http_status > 200
Number.toString()
Convert a value of type Number to type String.
Number.toString(number)
Example: print out numeric values
Note that multiple ways of printing numbers as part of a string are available. This example shows both Number.toString() conversion, and string interpolation without explicit conversion.
emit -from :0: -limit 10
| put value = count(), even = value%2
| reduce evens = count(even)
| put msg1 = "Count of even values: " + Number.toString(evens),
msg2 = "There are ${evens} even values"