我有一些代码可以格式化文件大小字符串:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setPositiveFormat: @"#,##0.## bytes"];
@"#,##0.## bytes"
是与 stringWithFormat 使用相同类型的格式字符串吗?这里的井号/加号符号是什么意思?我有一些代码可以格式化文件大小字符串:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setPositiveFormat: @"#,##0.## bytes"];
@"#,##0.## bytes"
是与 stringWithFormat 使用相同类型的格式字符串吗?这里的井号/加号符号是什么意思?# 通常会被数字替换,如果有一个数字存在,否则什么都不会替换。 0 将被数字替换,如果存在一个数字,否则将被替换为零。
所以对于以下格式“##00.00##”,你会得到以下输出:
1 => 01.00
12.1 => 12.10
1234.5 => 1234.50
1.2345 => 01.2345
'#'代表一个可选的数字位,如果该位置上是0,则不会出现在结果中,而0则表示该数字位始终会出现。
我们以345.5
为例:
#,##0.## = 345.5
0,000.00 = 0,345.50
#
'在大多数编程语言中被用作可选数字,而'0
'则是必需的数字(用于获取前导/尾随零)。