如何在左边用0填充一个整数?

时间:2022-05-26 21:27:12

I have the following variable:

我有以下变量:

NSNumber *consumption = [dict objectForKey:@"con"];

Which returns 42. How can I pad this number to 10 digits on the left, leading with zeros. The output should look as,

它返回42。我怎么能把这个数字加到左边的10位数,以0开头。输出应该是,

0000000042

0000000042

or if it were 420,

如果是420,

0000000420

0000000420

7 个解决方案

#1


95  

NSString *paddedStr = [NSString stringWithFormat:@"%010d", 42];

EDIT: It's C style formatting. %nd means the width is at least n. So if the integer is 2 digit long, then you will have length 3 string (when %3d is used). By default the left empty spaces are filled by space. %0nd (0 between % and n) means 0 is used for padding instead of space. Here n is the total length. If the integer is less than n digits then left padding is used.

编辑:这是C格式。%nd表示宽度至少为n。因此,如果该整数为2位长,那么您将拥有长度为3的字符串(使用%3d时)。默认情况下,左空白被空格填充。%0nd(%和n之间的0)表示0用于填充,而不是空格。这里n是总长度。如果该整数小于n位,则使用左填充。

#2


15  

The Objective-C way,

objective - c的方式,

NSNumberFormatter * numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setPaddingPosition:NSNumberFormatterPadBeforePrefix];
[numberFormatter setPaddingCharacter:@"0"];
[numberFormatter setMinimumIntegerDigits:10];

NSNumber * number = [NSNumber numberWithInt:42];

NSString * theString = [numberFormatter stringFromNumber:number];

NSLog(@"%@", theString);

The C way is faster though.

C方法更快。

#3


8  

You can't in the NSNumber itself. If you're creating a string from the number or using NSLog(), simply use the appropriate format, e.g.

你不能在NSNumber里面。如果您正在从数字或使用NSLog()创建字符串,只需使用适当的格式,例如。

NSLog(@"%010d", [consumption intValue]);

#4


3  

You can do pretty much any number formatting you would ever want with NSNumberFormatter. In this case I think you would want to use the setFormatWidth: and setPaddingCharacter: functions.

你可以用NSNumberFormatter做任何你想要的数字格式。在本例中,我认为您应该使用setFormatWidth:和setPaddingCharacter: functions。

#5


1  

with variable num_digits

与变量num_digits

NSString* format =[NSString stringWithFormat:@"%%0%zdzd", num_digits];
NSString *theString = [NSString stringWithFormat:format, 42];

#6


0  

E.g. Fill the rest with zeros, 5 digits:

用0、5位数填满剩下的数:

NSInteger someValue = 10;
[NSString stringWithFormat:@"%05ld", someValue];

Equivalent of .02f for float number when you need only 2 digits after the dot.

当你只需要2位数的时候,浮点数就等于0。02f。

So there you have 0 = fill with zeros, 5 = the number of digits and type.

这里有0 = 0,5 =数字和类型。

#7


0  

Solution for Swift 3

let x = 1078.1243
let numFormatter = NumberFormatter()
numFormatter.minimumFractionDigits = 1 // for float
numFormatter.maximumFractionDigits = 1 // for float
numFormatter.minimumIntegerDigits = 10 // how many digits do want before decimal
numFormatter.paddingPosition = .beforePrefix
numFormatter.paddingCharacter = "0"

let s = numFormatter.string(from: NSNumber(floatLiteral: x))!

OUTPUT

输出

"0000001078.1"

“0000001078.1”

#1


95  

NSString *paddedStr = [NSString stringWithFormat:@"%010d", 42];

EDIT: It's C style formatting. %nd means the width is at least n. So if the integer is 2 digit long, then you will have length 3 string (when %3d is used). By default the left empty spaces are filled by space. %0nd (0 between % and n) means 0 is used for padding instead of space. Here n is the total length. If the integer is less than n digits then left padding is used.

编辑:这是C格式。%nd表示宽度至少为n。因此,如果该整数为2位长,那么您将拥有长度为3的字符串(使用%3d时)。默认情况下,左空白被空格填充。%0nd(%和n之间的0)表示0用于填充,而不是空格。这里n是总长度。如果该整数小于n位,则使用左填充。

#2


15  

The Objective-C way,

objective - c的方式,

NSNumberFormatter * numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setPaddingPosition:NSNumberFormatterPadBeforePrefix];
[numberFormatter setPaddingCharacter:@"0"];
[numberFormatter setMinimumIntegerDigits:10];

NSNumber * number = [NSNumber numberWithInt:42];

NSString * theString = [numberFormatter stringFromNumber:number];

NSLog(@"%@", theString);

The C way is faster though.

C方法更快。

#3


8  

You can't in the NSNumber itself. If you're creating a string from the number or using NSLog(), simply use the appropriate format, e.g.

你不能在NSNumber里面。如果您正在从数字或使用NSLog()创建字符串,只需使用适当的格式,例如。

NSLog(@"%010d", [consumption intValue]);

#4


3  

You can do pretty much any number formatting you would ever want with NSNumberFormatter. In this case I think you would want to use the setFormatWidth: and setPaddingCharacter: functions.

你可以用NSNumberFormatter做任何你想要的数字格式。在本例中,我认为您应该使用setFormatWidth:和setPaddingCharacter: functions。

#5


1  

with variable num_digits

与变量num_digits

NSString* format =[NSString stringWithFormat:@"%%0%zdzd", num_digits];
NSString *theString = [NSString stringWithFormat:format, 42];

#6


0  

E.g. Fill the rest with zeros, 5 digits:

用0、5位数填满剩下的数:

NSInteger someValue = 10;
[NSString stringWithFormat:@"%05ld", someValue];

Equivalent of .02f for float number when you need only 2 digits after the dot.

当你只需要2位数的时候,浮点数就等于0。02f。

So there you have 0 = fill with zeros, 5 = the number of digits and type.

这里有0 = 0,5 =数字和类型。

#7


0  

Solution for Swift 3

let x = 1078.1243
let numFormatter = NumberFormatter()
numFormatter.minimumFractionDigits = 1 // for float
numFormatter.maximumFractionDigits = 1 // for float
numFormatter.minimumIntegerDigits = 10 // how many digits do want before decimal
numFormatter.paddingPosition = .beforePrefix
numFormatter.paddingCharacter = "0"

let s = numFormatter.string(from: NSNumber(floatLiteral: x))!

OUTPUT

输出

"0000001078.1"

“0000001078.1”