B
b converts a value to a big number.
Overview
Use b when you want the shorter form of big:
price | "19.99".b tax | "1.65".b total | price + tax :say(total)
price | "19.99".b tax | "1.65".b total | price + tax :say(total)
value.b, b(value), value.big, and big(value) perform the same conversion.
Method Style
The usual casting style places .b after the value:
price | "19.99".b distance | 250.b
price | "19.99".b distance | 250.b
The method form does not need parentheses.
The long name is available in the same style:
price | "19.99".big
price | "19.99".big
Use the free-call form when it reads more clearly around a larger expression:
price | b("19.99")
price | b("19.99")
Converting Numbers
b accepts integers, floats, percentages, and values that are already big numbers:
whole | 250.b decimal | 12.5.b
whole | 250.b decimal | 12.5.b
The result is a big number that can be used for high-precision decimal calculations.
When exact decimal values matter, converting from a string avoids carrying a float's binary representation into the result:
exact | "12.50".b
exact | "12.50".b
Converting Strings
A numeric string may contain an optional sign and one decimal point:
"1250".b "-42.75".b "+8.5".b
"1250".b "-42.75".b "+8.5".b
Whitespace around the number is ignored:
" 19.99 ".b /// same as "19.99".b
" 19.99 ".b /// same as "19.99".b
Underscores are also ignored, so they may be used to make large values easier to read:
population | "1_250_000".b
population | "1_250_000".b
A string ending in % is not accepted. Use Pct for percent strings.
Arrays and Maps
When given an array, b converts every value in the array:
prices | ["4.99", "12.50", "8.25"].b
prices | ["4.99", "12.50", "8.25"].b
When given a map, b converts every value and keeps the keys unchanged:
costs | { food: "24.50", travel: "80.00" }.b
costs | { food: "24.50", travel: "80.00" }.b
Every value inside the collection must be convertible to a big number.
Alias
b is an alias for big:
short_method | "123.45".b long_method | "123.45".big short_call | b("123.45") long_call | big("123.45")
short_method | "123.45".b long_method | "123.45".big short_call | b("123.45") long_call | big("123.45")
Choose the form that reads best in your program. See Big for the primary builtin page.
Signature
value.b b(value)
value.b b(value)
The equivalent long forms are value.big and big(value).
| Argument | Type | Description |
|---|---|---|
value |
value | A number, numeric string, array, or map to convert. |
.b returns a big number, or a collection whose values have been converted to big numbers.
Errors
b requires exactly one argument:
b() /// error: R0301 wrong-arity
b() /// error: R0301 wrong-arity
The value must be convertible to a big number:
b(true) /// error: R0215 type-lock-cast
b(true) /// error: R0215 type-lock-cast
A string must contain a valid decimal number:
b("twelve") /// error: R0215 type-lock-cast
b("twelve") /// error: R0215 type-lock-cast
Percent strings must be converted with pct instead:
b("12.5%") /// error: R0215 type-lock-cast
b("12.5%") /// error: R0215 type-lock-cast