A simple and efficient string formatting library for MoonBit.
Dependencies
struct TestObj {
name : String
age : Int
} derive(ToJson)
jprintln("Hello {:05} {}", [1, { name: "Alice", age: 30 }])
// Output: Hello 00001 {"name": String("Alice"), "age": Number(30)}let parts = "{0} {1} {{not things}} {1} {2:.2f}"
fprintln(parts, ["hello", "world", 42])
// Output: hello world {not things} world 42.00fprintln("{} {} {}", ["hello", "world", 42])
// Output: hello world 42fstring("{:o}", [8]) // "10"
fstring("{:#o}", [8]) // "0o10"
fstring("{:b}", [16]) // "10000"fstring("{:>>30}", ["test"]) // ">>>>>>>>>>>>>>>>>>>>>>test"
fstring("{:<<30}", ["test"]) // "test<<<<<<<<<<<<<<<<<<<<<<"
fstring("{:^^30}", ["test"]) // "^^^^^^^^^^^test^^^^^^^^^^^"fstring("{:_d}", [4285565, 59]) // "428_556_5"
fstring("{:,d}", [4285565, 3]) // "428,556,5"
fstring("{:#_b}", [4285565, 3]) // "0b1000_0010_1100_1000_1111_101"fstring("{:.5E}", [42.5]) // 4.25000E+01
fstring("{:.10e}", [4464252.51334]) // 4.4642525133e+06
fstring("{:.17e}", [4464252.51334]) // 4.46425251334000006e+06
| Syntax Example | Description | Output Example |
|---|---|---|
| {} | Automatic sequential argument | hello (argument: "hello") |
| {0} | Positional argument | world (argument: ["hello", "world"]) |
| {{ / }} | Output a single { or } | { or } |
| {:05} | Width 5, left pad with 0 | 00042 |
| {:>10} | Width 10, right align | test |
| {:<10} | Width 10, left align | test |
| {:^10} | Width 10, center align | test |
| {:*^10} | Center align, fill with * | ***test*** |
| {:b} / {:#b} | Binary / with prefix | 10000 / 0b10000 |
| {:o} / {:#o} | Octal / with prefix | 10 / 0o10 |
| {:x} / {:#x} | Hex lower / with prefix | ff / 0xff |
| {:X} / {:#X} | Hex upper / with prefix | FF / 0XFF |
| {:d} | Decimal integer | 42 |
| {:05d} | Width 5, left pad 0 decimal | 00042 |
| {:e} / {:E} | Scientific notation (lower/upper E) | 4.25e+1 / 4.25E+1 (argument: 42.5) |
| {:.2f} | Float with 2 decimal places | 3.14 (argument: 3.1415) |
| {:%} | Percent format, auto multiply by 100 | 4250.00% (argument: 42.5) |
| {:>>10} | Width 10, right align, fill > | >>>>>>>test |
| {:_d} | Decimal grouped with underscore | 428_556_5 |
| {:,d} | Decimal grouped with comma | 428,556,5 |
| {:#_b} | Base grouped with underscore & prefix | 0b1000_0010_1100_1000_1111_101 |
Note: Comma (,) as a grouping separator is only supported for decimal types; binary, octal, and hexadecimal only support underscore (_) as a separator.
pub(open) trait Formatter : Show {
format(Self, FormatSpec) -> String = _
display(Self, FormatSpec, Logger) -> Unit = _
}pub enum Align {
Left
Right
Center
}pub enum Grouping {
Comma
Underscore
Default
}pub enum SpecType {
Binary
Digit
ExponentLower
ExponentUpper
Float
Octal
HexLower
HexUpper
Percent
Default
}let name = "Alice"
let age = 30
@fmt.fprintln("Hello, {0}! You are {1} years old.", [name, age])
// Outputs: Hello, Alice! You are 30 years old.let name = "Alice"
let age = 30
let formatted = @fmt.fstring("Hello, {0}! You are {1} years old.", [name, age])
inspect(formatted, content="Hello, Alice! You are 30 years old.")let data = @json.parse("[\"Alice\", 30]")
@fmt.jprintln("Hello, {0}! You are {1} years old.", data)
// Outputs: Hello, Alice! You are 30 years old.let data = @json.parse("[\"Alice\", 30]")
let formatted = @fmt.jstring("Hello, {0}! You are {1} years old.", data)
inspect(formatted, content="Hello, Alice! You are 30 years old.")A simple and efficient string formatting library for MoonBit.
Dependencies