参考 nfp()

nfp()

Converts a Number into a String with a plus or minus sign.

nfp() converts numbers such as 123 into strings formatted with a + or - symbol to mark whether they're positive or negative, as in '+123'.

The first parameter, num, is the number to convert to a string. For example, calling nfp(123.45) returns the string '+123.45'. If an array of numbers is passed, as in nfp([123.45, -6.78]), an array of formatted strings will be returned.

The second parameter, left, is optional. If a number is passed, as in nfp(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 nfp(123.45, 4) returns the string '+0123.45'.

The third parameter, right, is also optional. If a number is passed, as in nfp(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 nfp(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 nfp(123.45, 4, 3) returns the string '+0123.450'.

示例

语法

nfp(num, [left], [right])
nfp(nums, [left], [right])

参数

num
Number:

number to format.

left
Integer:

number of digits to include to the left of the decimal point.

right
Integer:

number of digits to include to the right of the decimal point.

nums
Number[]:

numbers to format.

Returns

String: formatted string.
Notice any errors or typos? Please let us know. Please feel free to edit src/utilities/string_functions.js and open a pull request!

相关参考