Number prototype extensions
_ <- fat.type.Number
|Number||(val: Any)||Text to number or collection size|
performs the conversion from text to number assuming decimal base
|isEmpty||(): Boolean||Return true if zero|
|nonEmpty||(): Boolean||Return true if non-zero|
|size||(): Number||Return absolute value, same as math.abs|
|toText||(): Text||Return number as text|
|format||(fmt: Text): Text||Return number as formatted text|
_ <- fat.type.Number x = Number('52') # number: 52 x.toText # text: '52'
format method is used to convert numbers into strings in various ways. The basic structure of a format specifier is
%[flags][width][.precision][type]. Here's what each of these components mean:
flagsare optional characters that control specific formatting behavior. For example,
0can be used for zero-padding and
widthis an integer that specifies the minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces or zeros, depending on the flag used.
precisionis an optional number following a
.that specifies the number of digits to be printed after the decimal point.
typeis a character that specifies how the number should be represented. The common types are
g(either fixed or exponential depending on the magnitude of the number), and
a(hexadecimal floating-point notation).
%5.f: This will print the number with a total width of 5 characters, with no digits after the decimal point (because the precision is
f, which means fixed-point, but no number follows the dot). It will be right-justified because no
-flag is used.
%05.f: Similar to the above, but because the
0flag is used, the empty spaces will be filled with zeros.
%8.2f: This will print the number with a total width of 8 characters, with 2 digits after the decimal point.
%-8.2f: Similar to the above, but the number will be left-justified because of the
%.2e: This will print the number using exponential notation, with 2 digits after the decimal point.
%.2a: This will print the number using hexadecimal floating-point notation, with 2 digits after the hexadecimal point.
%.2g: This will print the number in either fixed-point or exponential notation, depending on its magnitude, with a maximum of 2 significant digits.