nf()

本参考资料用于Processing 3.0+。 如果你使用的是前一版本,请在使用软件帮助菜单内的参考资料。. If you see any errors or have suggestions, please let us know. If you prefer a more technical reference, visit the Processing Core Javadoc and Libraries Javadoc.

名称

nf()

示例

int a=200, b=40, c=90;

String sa = nf(a, 10);

println(sa);

// Prints "0000000200" String sb = nf(b, 5);

println(sb);

// Prints "00040" String sc = nf(c, 3);

println(sc);

// Prints "090" float d = 200.94, e = 40.2, f = 9.012;

String sd = nf(d, 10, 4);

println(sd);

// Prints "0000000200.9400" String se = nf(e, 5, 3);

println(se);

// Prints "00040.200" String sf = nf(f, 3, 5);

println(sf);

// Prints "009.01200" 
描述 Utility function for formatting numbers into strings. There are two versions: one for formattingfloats, and one for formatting ints. The values for the digits, left, and right parameters should always be positive integers.

As shown in the above example, nf() is used to add zeros to the left and/or right of a number. This is typically for aligning a list of numbers. To remove digits from afloating-point number, use the int(), ceil(), floor(), or round() functions.

语法
nf(num) nf(nums) nf(nums, digits) nf(num, digits) nf(nums, left, right) nf(num, left, right)
参数
nums float[], or
int[]: the numbers to format
digits int: number of digits to pad with zero
num float, or int: the number to format
left int: number of digits to the left of the decimal point
right int: number of digits to the right of the decimal point
返回值 String[]
相关 nfs()
nfp()
nfc()

Updated on October 2, 2018 05:02:07pm EDT

nf()