Integer
import "std/types" (included in import "std") · namespace std.types
Integer holds what whole numbers can do beyond arithmetic. Every static method whose first parameter is the number is also a method of any integer value, so n.toHex() and Integer.toHex(n) are the same direct call.
grav
import "std"
fn main() {
let n = 255
print(n.toHex() + " " + 255.toBinary())
print(Integer.parse("42") + 1)
print(1234567.grouped() + " " + 22.ordinal() + " " + 7.padded(3))
print("${(-7).abs()} ${12.gcd(18)} ${n.bitCount()} ${1000.nextPowerOfTwo()}")
let xs: Array<int> = [5, 3, 9]
xs.sortBy(Integer.descending)
print(xs)
try { print(Integer.MAX.addExact(1)) } catch (e: ArithmeticError) { print(e.message()) }
}ff 11111111
43
1,234,567 22nd 007
7 6 8 1024
[9, 5, 3]
integer overflow: 2147483647 + 1- Parsing:
Integer.parse(s)(an int, range-checked),parseLong,parse(s, base)(2 to 36;0xfftoo),parseOr,tryParse(null when it is not a number),isValid. A cast does the same:"42" as int. - Comparators (
compare,descending) fitsortByandSort; checked arithmetic (addExact,multiplyExact, ...) throwsArithmeticErrorinstead of wrapping. - int and long versions exist where the result's width matters (
abs,min,bitCount,rotateLeft), son.abs()stays an int for an int.
API
class Integer
Whole numbers (see the top of this file).
Static members
static readonly MAX: int = 2147483647— The largest and smallest int (32-bit).static readonly MIN: int = -2147483647 - 1static readonly LONG_MAX: long = 9223372036854775807— The largest and smallest long (64-bit).static readonly LONG_MIN: long = -9223372036854775807 - 1static fn parse(s: string) -> int—sas an int ("42", " -7 "); ValueError when it is not one or is out of range.static fn parseLong(s: string) -> long—sas a long; ValueError when it is not one.static fn parse(s: string, base: int) -> long—sinbase(2..36: "ff" in 16, "1010" in 2; base 16 also takes "0xff"); ValueError otherwise.static fn parseOr(s: string, fallback: long) -> long—sas a long, orfallbackwhen it is not one.static fn tryParse(s: string) -> long?—sas a long, or null when it is not one.static fn isValid(s: string) -> bool— Whether all ofsis a whole number.static fn toString(n: long) -> string— The number in decimal ("42", "-7").static fn toString(n: long, base: int) -> string— The number inbase(2..36), lower-case digits, with a "-" when negative.static fn toHex(n: long) -> string— Hexadecimal, lower case ("ff"; "-ff" for -255).static fn toBinary(n: long) -> string— Binary ("1010").static fn toOctal(n: long) -> string— Octal ("17").static fn grouped(n: long) -> string— With thousands separators ("1,234,567").static fn padded(n: long, width: int) -> string— Padded with zeros towidthdigits ("007"; a "-" comes first).static fn toChar(n: int) -> string— The character with code pointn("A" for 65), as a UTF-8 string.static fn ordinal(n: long) -> string— As an ordinal: "1st", "2nd", "3rd", "11th", "22nd".static fn abs(n: int) -> intstatic fn abs(n: long) -> longstatic fn sign(n: long) -> int— -1, 0 or 1.static fn min(a: int, b: int) -> intstatic fn min(a: long, b: long) -> longstatic fn max(a: int, b: int) -> intstatic fn max(a: long, b: long) -> longstatic fn clamp(n: int, lo: int, hi: int) -> int—nkept within lo..hi.static fn clamp(n: long, lo: long, hi: long) -> longstatic fn between(n: long, lo: long, hi: long) -> bool— Whether lo <= n <= hi.static fn isEven(n: long) -> boolstatic fn isOdd(n: long) -> boolstatic fn isPrime(n: long) -> bool— Whethernis prime.static fn gcd(a: long, b: long) -> long— The greatest common divisor (never negative).static fn lcm(a: long, b: long) -> long— The least common multiple (0 when either is 0).static fn pow(n: long, e: int) -> long—nto the powere(e >= 0); ArithmeticError when it overflows a long.static fn digitCount(n: long) -> int— How many decimal digitsnhas (the sign aside; 1 for 0).static fn digitSum(n: long) -> int— The sum of its decimal digits (the sign aside).static fn digits(n: long) -> Array<int>— Its decimal digits, most significant first (the sign aside).static fn times(n: int, f: fn(int))— Callsfwith 0, 1, ..., n - 1.static fn toFloat(n: long) -> float— The number as a float, a Decimal.static fn toDecimal(n: long) -> Decimalstatic fn compare(a: int, b: int) -> int— Comparators for sorting (xs.sortBy(Integer.compare)): -1, 0 or 1.static fn compare(a: long, b: long) -> intstatic fn descending(a: int, b: int) -> int— Largest first.static fn descending(a: long, b: long) -> intstatic fn compareTo(a: long, b: long) -> int—a.compareTo(b): as compare.static fn equals(a: long, b: long) -> boolstatic fn addExact(a: int, b: int) -> intstatic fn addExact(a: long, b: long) -> longstatic fn subtractExact(a: int, b: int) -> intstatic fn subtractExact(a: long, b: long) -> longstatic fn multiplyExact(a: int, b: int) -> intstatic fn multiplyExact(a: long, b: long) -> longstatic fn toIntExact(n: long) -> int—nas an int; ArithmeticError when it does not fit.static fn bitCount(n: int) -> int— How many bits are 1 (of the two's-complement bits: 64 of them for a long, 32 for an int).static fn bitCount(n: long) -> intstatic fn leadingZeros(n: int) -> int— Zero bits above the highest 1 (32 or 64 for 0).static fn leadingZeros(n: long) -> intstatic fn trailingZeros(n: int) -> int— Zero bits below the lowest 1 (32 or 64 for 0).static fn trailingZeros(n: long) -> intstatic fn isPowerOfTwo(n: long) -> bool— Whethernis a power of two (1, 2, 4, ...).static fn nextPowerOfTwo(n: long) -> long— The smallest power of two >= n (1 for n <= 1); ArithmeticError past a long.static fn rotateLeft(n: int, k: int) -> int— The bits turnedkplaces to the left (those leaving on the left come back on the right).static fn rotateLeft(n: long, k: int) -> longstatic fn rotateRight(n: int, k: int) -> intstatic fn rotateRight(n: long, k: int) -> longstatic fn testBit(n: long, i: int) -> bool— Whether biti(0 = the lowest) is 1.