NSNumberFormatter 和“th” “第一” 'nd' 'rd' (序数)数字结尾
有没有办法使用 NSNumberFormatter 来获取 'th' 'st' 'nd' 'rd' 数字结尾?
编辑:
看起来它不存在。这是我正在使用的。
+(NSString*)ordinalNumberFormat:(NSInteger)num{
NSString *ending;
int ones = num % 10;
int tens = floor(num / 10);
tens = tens % 10;
if(tens == 1){
ending = @"th";
}else {
switch (ones) {
case 1:
ending = @"st";
break;
case 2:
ending = @"nd";
break;
case 3:
ending = @"rd";
break;
default:
ending = @"th";
break;
}
}
return [NSString stringWithFormat:@"%d%@", num, ending];
}
改编自 nickf 的回答这里 .NET 中是否有一种简单的方法来获取数字的“st”、“nd”、“rd”和“th”结尾?
Is there a way to use NSNumberFormatter to get the 'th' 'st' 'nd' 'rd' number endings?
EDIT:
Looks like it does not exist. Here's what I'm using.
+(NSString*)ordinalNumberFormat:(NSInteger)num{
NSString *ending;
int ones = num % 10;
int tens = floor(num / 10);
tens = tens % 10;
if(tens == 1){
ending = @"th";
}else {
switch (ones) {
case 1:
ending = @"st";
break;
case 2:
ending = @"nd";
break;
case 3:
ending = @"rd";
break;
default:
ending = @"th";
break;
}
}
return [NSString stringWithFormat:@"%d%@", num, ending];
}
Adapted from nickf's answer here
Is there an easy way in .NET to get "st", "nd", "rd" and "th" endings for numbers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
从 iOS 9 开始执行此操作的正确方法是:
或者:
The correct way to do this from iOS 9 onwards, is:
Alternatively:
从 iOS 9
Swift 4
开始,最好将格式化程序放在扩展之外......
As of iOS 9
Swift 4
It's probably best to have the formatter outside the extension...
这用一种方法就可以解决问题(对于英语)。感谢 nickf https://stackoverflow.com/a/69284/1208690 的 PHP 原始代码,我只是将其改编为
目标C
:-This does the trick in one method (for English). Thanks nickf https://stackoverflow.com/a/69284/1208690 for original code in PHP, I just adapted it to
objective C
:-其他 Swift 解决方案不会产生正确的结果并且包含错误。
我已将 CmKndy 解决方案翻译为 Swift
测试结果:
0号
第一名
第二名
第三名
第四名
第五名
第六名
第七
8号
9号
10号
11号
12号
13号
14号
15号
16号
17日
18号
19日
20日
21日
22日
23日
Other Swift solutions do not produce correct result and contain mistakes.
I have translated CmKndy solution to Swift
test result:
0th
1st
2nd
3rd
4th
5th
6th
7th
8th
9th
10th
11th
12th
13th
14th
15th
16th
17th
18th
19th
20th
21st
22nd
23rd
由于问题要求使用数字格式化程序,因此这是我制作的一个粗略的格式化程序。
以及实现:
将其实例化为 Interface Builder 对象并将文本字段的格式化程序出口附加到它。为了更好地控制(例如设置最大值和最小值),您应该创建格式化程序的实例,根据需要设置属性并使用其
setFormatter:
方法将其附加到文本字段。您可以 从 GitHub 下载课程(包括示例项目)
Since the question asked for a number formatter, here's a rough one I made.
and the implementation:
Instantiate this as an Interface Builder object and attach the Text Field's formatter outlet to it. For finer control (such as setting maximum and minimum values, you should create an instance of the formatter, set the properties as you wish and attach it to text field using it's
setFormatter:
method.You can download the class from GitHub (including an example project)
-- 斯威夫特 4/5 --
-- Swift 4/5 --
用英语来说非常简单。这是一个快速扩展:
然后调用类似以下内容:
It's quite simple in English. Here's a swift extension:
Then call something like:
只需添加另一个实现作为类方法即可。直到我从 php 中的示例实现此问题后,我才看到这个问题。
Just adding another implementation as a class method. I didn't see this question posted until after I implemented this from an example in php.
这是一个适合所有整数类型的紧凑型 Swift 扩展:
示例用法:
输出:
Here's a compact Swift extension suitable for all integer types:
Example usage:
Output:
参考有一个简单的解决方案
对于这个Swift
Obj-c
: hackingwithswift.com
There is a simple solution for this
Swift
Obj-c
Referance: hackingwithswift.com
我不知道这种能力。但是,您可以自己执行此操作。在英语中,序数词(th、st、nd、rd 等)有一个非常简单的模式:
如果数字以:=>;使用:
这不会为您拼出该单词,但它允许您执行类似以下操作:“42nd”、“1,340,697th”等。
如果您需要本地化,这会变得更加复杂。
I'm not aware of this capability. However, it's possible to do this yourself. In English, the ordinal (th, st, nd, rd, etc) has a really simple pattern:
If the number ends with: => Use:
This will not spell out the word for you, but it will allow you to do something like: "42nd", "1,340,697th", etc.
This gets more complicated if you need it localized.
干净的 Swift 版本(仅适用于英语):
可以作为
Int
的扩展:A clean Swift version (for English only):
Can be done as an extension for
Int
:以下示例演示了如何处理任意数字。它是用 C# 编写的,但可以轻松转换为任何语言。
http://www.bytechaser.com/en/functions/b6yhfyxh78/convert-number-to-ordinal-like-1st-2nd-in-c-sharp.aspx
The following example demonstrates how to handle any number. It's in c# however it can easily converted to any language.
http://www.bytechaser.com/en/functions/b6yhfyxh78/convert-number-to-ordinal-like-1st-2nd-in-c-sharp.aspx
这会将日期转换为字符串,并在日期中添加序数。您可以通过更改 NSDateFormatter 对象来修改日期格式
This will convert date to string and also add ordinal in the date. You can modify the date formatte by changing NSDateFormatter object
这是一个 Swift 解决方案,它循环浏览用户的首选语言,直到找到具有已知规则(很容易添加)的序数规则:
Here's a Swift solution that cycles through the user's preferred languages until it finds one with known rules (which are pretty easy to add) for ordinal numbers:
不要忘记添加Locale,如果不添加,则不起作用。
用法:
Don't forget to add the Locale, if it is not added, it won't work.
Usage:
这里的许多解决方案不能处理像 112 这样的更高数字。这是一个简单的方法。
Many of the solutions here don't handle higher numbers like 112. Here is a simple way to do it.
这是英语的一个简短的 Int 扩展,它也正确地解释和显示负整数:
Here's a short Int extension for the English language that also accounts for and displays negative integers correctly:
你可以试试这个,它非常简化。
You can try this, Its well simplified.
这是我的暴力实现,采用 NSString* 表示日期并返回序数值。我觉得读起来容易多了。
This was my brute force implementation to taking a NSString* representation of the date and returning the ordinal value. I feel it's much easier to read.