ios 如何用零填充左侧的整数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6390706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:24:46  来源:igfitidea点击:

How can I pad an integer on the left with zeros?

objective-cioscocoa-touch

提问by George Johnston

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 位数字,以零开头。输出应如下所示,

0000000042

0000000042

or if it were 420,

或者如果是 420,

0000000420

0000000420

回答by taskinoor

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 nis the total length. If the integer is less than ndigits then left padding is used.

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

回答by Deepak Danduprolu

The Objective-Cway,

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 Cway is faster though.

C方法是快,但。

回答by puzzle

You can't in the NSNumberitself. 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]);

回答by David Brown

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:函数。

回答by dklt

with variable num_digits

带变量 num_digits

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

回答by Jakub Truhlá?

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

例如,用零、5 位数字填充其余部分:

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

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

.02f当点后只需要 2 位数字时,相当于浮点数。

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

所以你有 0 = 用零填充,5 = 数字和类型的数量。

回答by MANISH PATHAK

Solution for Swift 3

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”