Converts a Number
into a String
with a given number of digits.
nf()
converts numbers such as 123.45
into strings formatted with a set number of digits, as in '123.4500'
.
The first parameter, num
, is the number to convert to a string. For example, calling nf(123.45)
returns the string '123.45'
. If an array of numbers is passed, as in nf([123.45, 67.89])
, an array of formatted strings will be returned.
The second parameter, left
, is optional. If a number is passed, as in nf(123.45, 4)
, it sets the minimum number of digits to include to the left of the decimal place. If left
is larger than the number of digits in num
, then unused digits will be set to 0. For example, calling nf(123.45, 4)
returns the string '0123.45'
.
The third parameter, right
, is also optional. If a number is passed, as in nf(123.45, 4, 1)
, it sets the minimum number of digits to include to the right of the decimal place. If right
is smaller than the number of decimal places in num
, then num
will be rounded to the given number of decimal places. For example, calling nf(123.45, 4, 1)
returns the string '0123.5'
. If right is larger than the number of decimal places in num
, then unused decimal places will be set to 0. For example, calling nf(123.45, 4, 3)
returns the string '0123.450'
.
When the number is negative, for example, calling nf(-123.45, 5, 2)
returns the string '-00123.45'
.
Examples
Syntax
nf(num, [left], [right])
nf(nums, [left], [right])
Parameters
number to format.
number of digits to include to the left of the decimal point.
number of digits to include to the right of the decimal point.
numbers to format.