stringlib::formatf
-- Convert a
floating point number to a stringstringlib::formatf(
f, d)
converts the
floating point number f
into a string after rounding it to
d
digits after the decimal point.
stringlib::formatf(f, digits <, strlength>)
f |
- | floating point number |
digits |
- | integer which determines the precision of the number |
strlength |
- | integer which determines the length of the returned string |
stringlib::formatf
returns a string.
d
is a positive integer, a rounded fixed-point
representation with d
digits after the decimal point is
returned. If d
is zero, then a rounded fixed-point
representation with one zero after the decimal point is returned. If
d
is negative, then f
is rounded to
-d
digits before the decimal point and a fixed-point
representation with one zero after the decimal point is returned.strlength
characters. If the converted number
f
requires less room, then it is padded on the left with
spaces. If the converted number f
requires more room, then
the last characters are truncated.Convert the number 123.456
with two
characters after the point into a string:
>> stringlib::formatf(123.456, 2)
" 123.46"
The same for -123.456
:
>> stringlib::formatf(-123.456, 2)
"-123.46"
Convert the number 123.456
with two
characters after the point into a string of the length
10
:
>> stringlib::formatf(123.456, 2, 10)
" 123.46"
If the string should only have the length
3
, the whole number does not fit into the string:
>> stringlib::formatf(123.456, 2, 3)
" 12"
Rounding to no number after point:
>> stringlib::formatf(123.456, 0)
" 123.0"
Rounding to one number in front of point:
>> stringlib::formatf(123.456, -1)
" 120.0"
string::formatf