NSString stringwithformat:填充 2 个长度未知的字符串 [英] NSString stringwithformat: Padding 2 strings with unknown length

查看:71
本文介绍了NSString stringwithformat:填充 2 个长度未知的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理 NSString stringWithFormat 时遇到问题...

I'm having a problem doing something with NSString stringWithFormat...

问题如下:

首先,我有 2 个字符串.哪些是货币数量.

First of all, I have 2 strings. Which are money quantities.

问题是,我想用这两个创建 1 个字符串.

The thing, is that i want to create 1 string with these two.

例如:

strF = [NSString stringWithFormat:@"First: %@   Second: %@", value1, value2];

我想知道是否可以在 First 和 Second 之间进行正确的填充.. 假设第一个字符串的长度总是不同..

I want to know if there is possible to make a correct padding between First and Second.. Assuming the first string is not the same length always..

我想做这个:

First: 10,000.00         Second: 788.00
First: 10.00             Second: 788.00
First: 0.00              Second: 788.00
First: 100.00            Second: 788.00
First: 5.00              Second: 788.00

推荐答案

revision 这里有一个更好的方法,使用 NSNumberFormatter:

revision Here's a better way, using NSNumberFormatter:

首先像这样创建和配置格式化程序:

First create and configure your formatter like this:

NSNumberFormatter * formatter = [[ NSNumberFormatter alloc ] init ] ;
[ formatter setFormatWidth:20 ] ;
[ formatter setPaddingCharacter:@" " ] ;
[ formatter setFormatterBehavior:NSNumberFormatterBehavior10_4 ] ;
[ formatter setNumberStyle:NSNumberFormatterDecimalStyle ] ;
[ formatter setMinimumFractionDigits:2 ] ;
[ formatter setMaximumFractionDigits:2 ] ;

创建格式化程序后,您的代码将变为:

Once you have created your formatter, your code becomes:

strF = [ NSString stringWithFormat:@"First: %@ Second: %@", [ formatter stringFromNumber:value1 ], [ formatter stringFromNumber:value2 ] ] ;

您将获得(数千个)分隔符(在当前语言环境中)等.

You'll get the (thousands) separators (in the current locale) etc.

我会在类方法中创建格式化程序(类似于 +numberFormatter 或在 dispatch_once 块中)你甚至可以通过一个类别将它添加到 NSNumberFormatter 并有一些东西像 +[ NSNumberFormatter mySharedFormatter ]

I would create the formatter in a class method (something like +numberFormatter or in a dispatch_once block) You could even add it to NSNumberFormatter via a category and have something like +[ NSNumberFormatter mySharedFormatter ]

您可以使用字段宽度说明符.它这样做:

You can use the field width specifier. It does this:

NSString * value1String = ... from NSNumberFormatter to get commas inserted ;
[ NSString stringWithFormat:@"First: %20s Second: %0.2f", [ value1String UTF8String ], value2 ]

其中20"是列的宽度.(替换为所需的宽度).还有其他选项,例如右对齐等.更多信息请参阅 man printf.

Where "20" is the width of the column. (Replace by desired width). There's other options, like right justify, etc. Do man printf for more.

如果你想添加逗号,NSNumberFormatter 应该可以做到.

If you want to add the commas, NSNumberFormatter should be able to do that.

(实际上,您可能也希望小数点对齐,对吗?您可以试试 % 20.2f)

(Actually, you probably want your decimals to line up too, right? You could try % 20.2f)

这篇关于NSString stringwithformat:填充 2 个长度未知的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆