Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 目标C中的java.text.DecimalFormat等价物_Objective C_Decimalformat - Fatal编程技术网

Objective c 目标C中的java.text.DecimalFormat等价物

Objective c 目标C中的java.text.DecimalFormat等价物,objective-c,decimalformat,Objective C,Decimalformat,在java中,我有 String snumber = null; String mask = "000000000000"; DecimalFormat df = new DecimalFormat(mask); snumber = df.format(number); //'number' is of type 'long' passed to a function //which has this code in it 我不知道

在java中,我有

String snumber = null;
String mask = "000000000000";
DecimalFormat df = new DecimalFormat(mask);
snumber = df.format(number);  //'number' is of type 'long' passed to a function
                              //which has this code in it
我不知道java中的
DecimalFormat
操作,因此很难编写等效的OBJC代码


我怎样才能做到这一点?任何帮助都将不胜感激。

对于这种特殊情况,您可以在Objective-C中使用一些C风格的魔法:

long number = 123;
int desiredLength = 10;

NSString *format  = [NSString stringWithFormat:@"%%0%dd", desiredLength];
NSString *snumber = [NSString stringWithFormat:format, number];
结果是
0000000123


此处的格式为
%010d

10d
表示数字将有10个点向右对齐。
0
开头的原因是所有“空”点将用0填充。
若数字短于所需长度,则按原样格式化(无前导零)。

当然,上述代码仅在您希望数字具有指定长度且空格由零填充时有效。
对于其他场景,例如,您可以编写自己的自定义类,该类将使用适当的printf/NSLog格式生成您希望的格式字符串。
在Objective-C中,您必须使用字符串格式,而不是使用十进制格式的“掩码”