首页 > 文档 > nf()格式化成字符串
2017
07-21

nf()格式化成字符串

Name

nf()格式化成字符串

   

Examples

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”

Description

Utility function for formatting numbers into strings. There are two versions: one for formatting floats, and one for formatting ints. The values for the digitsleft, 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 a floating-point number, use the int()ceil()floor(), or round() functions.

用于将数字格式化为字符串的实用程序函数。有两个版本: 一个用于格式化浮动, 一个用于格式化整数。数字、左和右参数的值应始终为正整数。

 

如上例所示, nf () 用于在数字的左边和/或右边添加零。这通常用于对齐数字列表。若要从 floating-point 数字中移除数字, 请使用 int ()、天花板 ()、底板 () 或圆形 () 函数。

Syntax

nf(num)

nf(nums)

nf(nums, digits)

nf(num, digits)

nf(nums, left, right)

nf(num, left, right)

Parameters

nums

float[], or int[]: the numbers to format

浮点 [] int []: 要格式化的数字

digits

int: number of digits to pad with zero

int: 要填充为零的位数

num

float, or int: the number to format

浮点或 int: 要格式化的数字

left

int: number of digits to the left of the decimal point int: 小数点左边的位数

right

int: number of digits to the right of the decimal point int: 小数点右边的位数

Returns

String[]

Related

nfs()
nfp()
nfc()



最后编辑:
作者:卡萨布兰卡
这个作者貌似有点懒,什么都没有留下。

留下一个回复

你的email不会被公开。