Numeric
num_parse¶
Parse a string into a base-10 integer. Auto-detects numeric representation from prefix or format, or accepts an explicit source base. Handles: decimal, hex (0x/0X), binary (0b/0B), octal (0o/0O), scientific notation, floats (truncated).
Example¶
num_parse "0xFF" # => 255
num_parse "FF" 16 # => 255
num_parse "1.5e3" # => 1500
num_parse "3.7" # => 3
Arguments¶
- $1 (string): The value to parse
- $2 (int): Optional: source base for the input string (default: auto-detect)
Exit codes¶
- 0: Always
- 1: No input given
Output on stdout¶
- Base-10 integer
int¶
Convenience alias for num_parse(). Parses a string to a base-10 integer. Equivalent to Go's strconv.Atoi.
Arguments¶
- ... (Forwarded): to num_parse
Exit codes¶
- 0: Always
- 1: No input given
Output on stdout¶
- Base-10 integer
num_format¶
Format an integer in the given base. Supports bases 2-36. Bases 8, 10, and 16 use printf directly; others use a digit-string loop.
Example¶
num_format 255 16 # => ff
num_format 255 2 # => 11111111
num_format 255 8 # => 377
Arguments¶
- $1 (int): The integer to format
- $2 (int): Target base (default: 10)
Exit codes¶
- 0: Always
- 1: No input given
Output on stdout¶
- Integer represented in the target base
num_2dp¶
Format one or more numbers to two decimal places.
Arguments¶
- ... (number): One or more numeric values
Exit codes¶
- 0: Always
Output on stdout¶
- Each value formatted to two decimal places, one per line
num_thousands¶
Format an integer with thousands separators (commas).
Example¶
num_thousands 1234567 # => 1,234,567
num_thousands -9876543 # => -9,876,543
num_thousands 999 # => 999
Arguments¶
- $1 (int): Integer to format
Exit codes¶
- 0: Success
- 1: No argument supplied
Output on stdout¶
- Formatted integer string
num_zeropad_right¶
Right-pad an integer with zeros to reach a minimum length. If the integer is already at or above the target length, it is printed unchanged.
Arguments¶
- $1 (int): The integer to pad
- $2 (int): Optional: target minimum length (default: 3)
Exit codes¶
- 0: Always
Output on stdout¶
- Zero-right-padded integer
num_is_integer¶
Test whether a value can be interpreted as an integer.
Two modes, selected by optional first argument:
Default (printf probe): passes if printf %d can parse the value. Accepts signed integers; rejects leading quote characters that printf %d would otherwise treat as an ASCII codepoint.
--regex (strict pattern): passes only if the value matches ^[+-]?[0-9]+$. No format coercions; rejects hex, octal, scientific notation. Use this when you need to validate raw user input.
Example¶
num_is_integer "42" # => 0 (true)
num_is_integer -- "-5" # => 0 (true)
num_is_integer "1e5" # => 1 (false; printf %d rejects it)
num_is_integer --regex "42" # => 0 (true)
num_is_integer --regex "-5" # => 0 (true)
num_is_integer --regex "1e5" # => 1 (false)
Options¶
- [--regex] flag Use strict regex matching instead of printf
Arguments¶
- $1 (string): Value to test
Exit codes¶
- 0: Value is an integer
- 1: Value is not an integer
num_is_float¶
Test whether a value can be interpreted as a float.
Two modes, selected by optional first argument:
Default (printf probe): passes if printf %f can parse the value. Accepts scientific notation (1e5), plain integers, decimals with leading or trailing dot (.5, 5.).
--regex (strict pattern): passes only if the value matches ^[+-]?[0-9]+.?[0-9]*$. Rejects scientific notation and leading-dot decimals. Use this when you need to validate a specific decimal format.
Example¶
num_is_float "1.5" # => 0 (true)
num_is_float "1e5" # => 0 (true; printf %f accepts it)
num_is_float ".5" # => 0 (true; printf %f accepts it)
num_is_float --regex "1.5" # => 0 (true)
num_is_float --regex "1e5" # => 1 (false; regex rejects scientific notation)
num_is_float --regex ".5" # => 1 (false; regex requires leading digit)
Options¶
- [--regex] flag Use strict regex matching instead of printf
Arguments¶
- $1 (string): Value to test
Exit codes¶
- 0: Value is a float
- 1: Value is not a float
num_is_numeric¶
Test whether a value is a non-negative integer (digits only, no sign). Uses strict regex: ^[0-9]+$. Does not accept leading +/-. Equivalent to Python's str.isdigit() for integer strings. Returns exit 2 if called with no argument.
Example¶
num_is_numeric "42" # => 0 (true)
num_is_numeric "0" # => 0 (true)
num_is_numeric "-1" # => 1 (false; sign not allowed)
num_is_numeric "1.5" # => 1 (false)
Arguments¶
- $1 (string): Value to test
Exit codes¶
- 0: Value is a non-negative integer
- 1: Value is not
- 2: Missing argument
num_is_positive_integer¶
Test whether a value is a positive integer (1 or greater, no sign). Uses strict regex: ^[1-9][0-9]*$. Useful for validating array indices, counts. Returns exit 2 if called with no argument.
Example¶
num_is_positive_integer "1" # => 0 (true)
num_is_positive_integer "0" # => 1 (false; zero is not positive)
num_is_positive_integer "-1" # => 1 (false)
num_is_positive_integer "01" # => 1 (false; leading zero not allowed)
Arguments¶
- $1 (string): Value to test
Exit codes¶
- 0: Value is a positive integer
- 1: Value is not
- 2: Missing argument
num_is_odd¶
Test whether an integer is odd.
Arguments¶
- $1 (int): Integer to test
Exit codes¶
- 0: Number is odd
- 1: Number is even
num_is_even¶
Test whether an integer is even.
Arguments¶
- $1 (int): Integer to test
Exit codes¶
- 0: Number is even
- 1: Number is odd