Format
import "std/text" (included in import "std") · namespace std.text
Format converts strings to values and values to formatted strings. (For the other direction in general, the built-in str(x) turns any value into text, and ${x:spec} formats numbers inline.)
grav
import "std"
fn main() {
print(Format.toInt("42") + 1)
print(Format.toFloat("2.5") * 2)
print(Format.toBool("yes"))
print(Format.isInt("12x"))
print(Format.toIntOr("oops", -1))
print(Format.parseInt("ff", 16, 0))
print(Format.fixed(3.14159, 2))
print(Format.grouped(1234567))
print(Format.percent(0.256, 1))
print(Format.hex(255))
print(Format.binary(5))
print(Format.bytes(1536))
}43
5
true
false
-1
255
3.14
1,234,567
25.6%
0xff
0b101
1.5 KBParsing rules to know:
toInt,toLong,toUIntandtoByteaccept a whole number, with spaces around it allowed, and throwValueErrorfor anything else ("12a","x","") or for a number that does not fit the type.toFloatandtoDoubleaccept any decimal number ("2.5","1e3") and throwValueErrorotherwise.- To avoid the exception, check with
isInt/isNumberfirst, or usetoIntOr(s, fallback),toLongOrandtoFloatOr. toBoolistruefor"true","1","yes","y"and"on"in any case.
API
class Format
Parses strings into primitive values.
Static members
static fn toInt(s: string) -> int— The integer parsers take a whole number (spaces around it are allowed) and throw ValueError for anything else ("12a", "x", "") or a number that does not fit the result type. toIntOr / isInt avoid the exception.static fn toLong(s: string) -> long— Parses a whole number; ValueError when the text is not one or it does not fit in a long.static fn toUInt(s: string) -> uint— Parses a whole number into a uint; ValueError when it is negative or too big.static fn toByte(s: string) -> byte— Parses a whole number into a byte; ValueError outside 0..255.static fn toFloat(s: string) -> float— Parses a decimal number; ValueError when the text is not one.static fn toDouble(s: string) -> double— Parses a decimal number; ValueError when the text is not one.static fn toBool(s: string) -> bool— true for "true", "1", "yes", "y", "on" (any case); false otherwise.static fn isInt(s: string) -> bool— Whether the text (spaces around allowed) is exactly one integer.static fn isNumber(s: string) -> bool— Whether the text (spaces around allowed) is exactly one number.static fn toIntOr(s: string, fallback: int) -> int— Parse, or returnfallbackunless the whole text is a valid number.static fn toLongOr(s: string, fallback: long) -> long— Parses a long, or returnsfallbackunless the text is a valid long.static fn toFloatOr(s: string, fallback: float) -> float— Parses a float, or returnsfallbackunless the text is a valid number.static fn parseInt(s: string, base: int, fallback: long) -> long— Parse in another base: Format.parseInt("ff", 16) == 255 (fallback if invalid).static fn toStr(n: int) -> string— The number as text (same as str(n)).static fn hex(n: int) -> string— The number in hexadecimal: 255 -> "0xff".static fn binary(n: long) -> string— The number in binary: 5 -> "0b101".static fn octal(n: long) -> string— The number in octal: 8 -> "0o10".static fn fixed(x: float, digits: int) -> string— A float with exactlydigitsdecimals: Format.fixed(3.14159, 2) == "3.14".static fn grouped(n: long) -> string— Thousands separators: Format.grouped(1234567) == "1,234,567".static fn percent(ratio: float, digits: int) -> string— A ratio as a percentage: Format.percent(0.256, 1) == "25.6%".static fn bytes(n: long) -> string— Bytes as a human-readable size: Format.bytes(1536) == "1.5 KB".static fn intToFloat(n: int) -> float— The int as a float.static fn floatToInt(f: float) -> int— The float as an int (the fraction is dropped).static fn intToLong(n: int) -> long— The int as a long.static fn longToInt(n: long) -> int— The long as an int (wraps when out of range).static fn intToByte(n: int) -> byte— The low 8 bits of the int, as a byte.static fn intToDouble(n: int) -> double— The int as a double.static fn toBinary(s: string) -> binary— The string's bytes as a binary value.static fn fromBinary(b: binary) -> string— The bytes as a string (up to the first zero byte).