- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
我的 iOS 应用在不同的本地化版本(en_US
、en_AU
、ja_JP
等)中显示不同的货币(USD、JPY、AUD、EUR) ).
对于日语地区/语言(两者都在我的设备上设置),如果我:
NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init];
fmt.numberStyle = NSNumberFormatterCurrencyStyle;
fmt.currencyCode = @"JPY";
NSString *labelText = [fmt stringFromNumber:@1000000];
我的标签文本是 ¥1,000,000
。但是,在日文和中文中,大于 10,000 的数字可能会写成 100 万日元
,这就是我想要的输出。
知道我可以编写什么代码来获得 100 万日元
作为输出吗?
我想避免在我的代码中检查语言环境/区域的逻辑 block ,但我觉得这就是我要面对的(例如,使用方法调用 fmt.multipler = @(1/10000)
将 1,000,000 除以 10,000 以获得正确的值)。
最佳答案
编辑:这里是最新的要点:https://gist.github.com/fjolnir/cd72ea39be1476023adf
旧线程,但我在寻找解决方案时遇到了它,所以我想我应该发布我的实现。
格式化程序本身不处理日元的放置,但在它之外很容易做到。 (如下例所示)
下面的预期输出是:
2015-03-11 18:00:13.376 LENumberFormatter[82736:3604947] 12億3,460万円
2015-03-11 18:00:13.377 LENumberFormatter[82736:3604947] 25円
-
@import Foundation;
@import ObjectiveC.message;
typedef NS_ENUM(NSUInteger, LENumberFormatterAbbreviationStyle) {
kLEAbbreviateShort, // 2.5m
kLEAbbreviateNormal // 2m 5k
};
@interface LENumberFormatter : NSNumberFormatter
@property(nonatomic) BOOL abbreviateLargeNumbers;
@property(nonatomic) LENumberFormatterAbbreviationStyle abbreviationStyle;
@end
@implementation LENumberFormatter
- (instancetype)init
{
if((self = [super init])) {
self.abbreviationStyle = [self _usingKanjiNumbers]
? kLEAbbreviateNormal
: kLEAbbreviateShort;
}
return self;
}
- (NSString *)stringForObjectValue:(id const)aObj
{
if(!_abbreviateLargeNumbers || ![aObj isKindOfClass:[NSNumber class]])
return [super stringForObjectValue:aObj];
// Copy ourselves to get format the partial digits using the settings on self
LENumberFormatter * const partialFormatter = [self copy];
partialFormatter.currencySymbol = @"";
if(_abbreviationStyle == kLEAbbreviateNormal)
partialFormatter.maximumFractionDigits = 0;
NSString *(^partialFormat)(NSNumber*) = ^(NSNumber *num) {
NSString *(*superImp)(struct objc_super*,SEL,NSNumber*) = (void*)&objc_msgSendSuper;
return superImp(&(struct objc_super) { partialFormatter, self.superclass }, _cmd, num);
};
double n = [aObj doubleValue];
BOOL const shortFormat = _abbreviationStyle == kLEAbbreviateShort;
NSDictionary * const separators = [self _localizedGroupingSeparators];
NSArray * const separatorExponents = [separators.allKeys sortedArrayUsingSelector:@selector(compare:)];
BOOL const currencySymbolIsSuffix = [self.positiveFormat hasSuffix:@"¤"];
NSMutableString * const result = currencySymbolIsSuffix || self.numberStyle != NSNumberFormatterCurrencyStyle
? [NSMutableString new]
: [self.currencySymbol mutableCopy];
NSUInteger significantDigits = 0;
NSNumber *lastExp = nil;
for(NSNumber *exp in separatorExponents.reverseObjectEnumerator) {
double divisor = pow(10, exp.shortValue);
if(divisor > n)
continue;
if(lastExp)
significantDigits += lastExp.doubleValue - exp.doubleValue;
lastExp = exp;
if(self.usesSignificantDigits && significantDigits >= self.maximumSignificantDigits)
break;
double partialNum = shortFormat
? n/divisor
: floor(n/divisor);
NSString * const digits = [self _groupRecursively] && ![exp isEqual:@0]
? [partialFormatter stringFromNumber:@(partialNum)]
: partialFormat(@(partialNum));
[result appendFormat:@"%@%@", digits, separators[exp]];
n = fmod(n, divisor);
if(shortFormat)
break; // Just use a float+first hit
// If we make it here, partialNum is integral and we can use log10 to find the number of digits
significantDigits += log10(partialNum) + 1;
partialFormatter.maximumSignificantDigits -= digits.length;
}
if(n > 0
&& !shortFormat
&& (!self.usesSignificantDigits || significantDigits < self.maximumSignificantDigits))
{
partialFormatter.maximumFractionDigits = self.maximumFractionDigits;
[result appendString:partialFormat(@(n))];
}
if(self.numberStyle == NSNumberFormatterCurrencyStyle && currencySymbolIsSuffix && self.currencySymbol)
[result appendString:self.currencySymbol];
return result.length > 0
? [result stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet]
: [super stringForObjectValue:aObj];
}
- (BOOL)_usingKanjiNumbers
{
return [self.locale.localeIdentifier rangeOfString:@"^(ja|zh)_"
options:NSRegularExpressionSearch].location != NSNotFound;
}
- (NSDictionary *)_localizedGroupingSeparators
{
if(self._usingKanjiNumbers)
return @{ @2: @"百", @3: @"千", @4: @"万", @8: @"億" };
else {
NSBundle * const bundle = [NSBundle bundleForClass:self.class];
return @{
@3: [bundle localizedStringForKey:@"thousandSuffix" value:@"k " table:nil],
@6: [bundle localizedStringForKey:@"millionSuffix" value:@"m " table:nil]
};
}
}
- (BOOL)_groupRecursively
{
// Return _usingKanjiNumbers if you want:
// 12億3千4百56万7千8百90
// Rather than:
// 1億2,3456万7千8百90
return NO;
}
- (instancetype)copyWithZone:(NSZone * const)aZone
{
LENumberFormatter * const copy = [super copyWithZone:aZone];
copy.abbreviateLargeNumbers = _abbreviateLargeNumbers;
copy.abbreviationStyle = _abbreviationStyle;
return copy;
}
@end
int main(int argc, char *argv[]) {
@autoreleasepool {
LENumberFormatter * const f = [LENumberFormatter new];
f.locale = [NSLocale localeWithLocaleIdentifier:@"ja_JP"];
// f.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
f.numberStyle = NSNumberFormatterCurrencyStyle;
f.abbreviateLargeNumbers = YES;
f.abbreviationStyle = kLEAbbreviateNormal; // Automatic if using system locale
f.maximumSignificantDigits = 5;
f.usesSignificantDigits = YES;
// f.currencyCode = @"JPY";
// f.currencySymbol = @"¥";
if([f.locale.localeIdentifier hasPrefix:@"ja"]) {
f.positiveFormat = @"#,##0¤";
if([f.currencyCode isEqualToString:@"JPY"])
// We allow ourselves this special case because *日本円 just looks dumb
f.currencySymbol = @"円";
else
f.currencySymbol = [f.locale displayNameForKey:NSLocaleCurrencyCode
value:f.currencyCode];
}
NSLog(@"%@", [f stringFromNumber:@1234567890]);
NSLog(@"%@", [f stringFromNumber:@25]);
}
}
关于ios - 如何设置 NSNumberFormatter 以使用 "万"(日文/中文 10,000 标记)显示数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14471579/
这个问题在这里已经有了答案: Inappropriate Expected declaration error (1 个回答) 关闭 7 年前。 我正在阅读我的 swift 书并边做边建。我在使用这
这是我的代码片段,我不知道如何舍入双数。 double m = [tmpProduct.msrp doubleValue] ; double d = [tmpProduct.discountPric
它看起来像是NSNumberFormatter 的 方法,用于设置自定义格式化程序。 setFormat: 在 iPhone 上不可用... 所以这......使我的应用程序崩溃: NSNumberF
我在 NSTableView 的单元格中使用了 NSNUmberFormatter。这个 NSNUmberFormatter 是从 IB 库中拖出来并放入 TableView 单元格中的。然后从检查器
我对 NSNumberFormatter 的使用感到完全困惑。这应该非常简单,但我无法让它工作。 我想设置一个 NSTextField 以允许输入带小数点或不带小数点的十进制数字。这是我认为可行的:
我想将 NSDecimalNumber 格式化为美元值(1.50 美元),但我遇到了崩溃。 这是我的方法: +(NSString*) formatPriceForUser:(NSDecimalNumb
以下代码无法正常工作: //[formatter setNumberStyle:NSNumberFormatterDecimalStyle]; formatter.maximumFractionDig
我正在开发一个适用于 double 的计算器应用程序,然后当您按下等于时,操作是使用 double 完成的,然后切换到 NSNumberFormatter。我的问题是当用户想要在屏幕上的答案中添加(或
有人问过类似的问题,但我发现提供的解决方案不适用于最新的 iOS SDK,或者不完全适用于这种特殊情况。 我使用 NSNotification 调用一个方法,该方法在 UILabel 中使用分组符号格
我的应用程序中有一个价格字段。我使用了 NSNumberFormatter 并且我正在使用核心数据存储和检索值。它在模拟器上运行良好,但设备上没有显示任何内容。 我还显示了在模拟器上运行正常但在设备上
我的问题是我目前使用的是 NSNumberFormatter 和 NSNumberFormatterDecimalStyle。我使用它是因为我想让逗号将每三个数字的数字隔开,就像当我把它变成一个字符串
我将收到美国 5.00 美元、日本 5.00 日元、欧洲 5.00 欧元等。但我不想要那样。我想在每个地区都获得相同的东西:美国 5.00 美元,日本 5.00 美元,欧洲 5.00 美元。这是我尝试
我有一个字符串,其货币格式为本地货币。例如10000 丹麦克朗 将 (10.000,00)当转换为双倍值时,它只读为 10。我试过这个片段 NSString * locale ; if(loc
我的代码是: NSString *price = @"0.29000"; NSNumberFormatter *f = [[NSNumberFormatter alloc] init]; [f set
我正在制作一个包含多个文本字段的应用程序。我希望当用户在文本字段中输入数字时,编辑结束时自动添加百分比符号。现在,我知道如何做到这一点的唯一方法是将文本值转换为 float ,然后使用 NSNumbe
我已经设置了一个 NSNumberFormatter 如下: formatter = [[NSNumberFormatter alloc] init]; [formatter setNumberSty
我需要格式化数字和货币符号,我使用以下代码。我面临的问题是该数字四舍五入为 34.80,这仅适用于某些货币,如果我在下面的代码中将货币代码更改为“INR”,结果显示的金额没有四舍五入。 NSDecim
处理整数、NSString 和 NSNumberFormatter 的最佳方法是什么? 我有一个带有 UITextField 的应用程序,用户可以在其中输入数字。通过 UITextField 中的每一
我正在尝试获取 NSNumberFormatter 来显示货币。我有两个 UILabels,两者都由相同的 NSNumberFormatter 格式化,但第一个被括号包围。这是我的代码: NS
我有一个奇怪的问题。它似乎只出现在测试者的 iPhone 5s 上。它可以在运行最新 iOS (8.3) 的 iPhone 5、6 和 6 plus 上正常运行。 这是代码 -(NSString *)
我是一名优秀的程序员,十分优秀!