NSLocale货币符号,显示金额值之前或之后 [英] NSLocale currency symbol, show before or after amount value

查看:745
本文介绍了NSLocale货币符号,显示金额值之前或之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 StoreKit 在我的应用程序中实现应用程序内购买商店。

I am using StoreKit to implement an in app purchase store in my application.

我有一个自定义设计,它意味着价格的价值应该是白色和大,货币符号更小,更暗,并与价格值的顶部对齐。

I have a custom design and it means that the value of the price should be white and large, and the currency symbol smaller, darker and aligned to the top of the price value.

我可以在 SKproduct priceLocale中使用 NSLocale 获取货币符号没有任何问题属性,以及价格属性中的价格值。

I can get the currency symbol without any problems by using the NSLocale in SKproduct's priceLocale property, and the value of the price in the price property.

我的问题是知道当我应该在价格之前加上货币符号以及何时将货币符​​号放在价格之后。

My problem is knowing when I should put the currency symbol before the price and when to put it after the price.

示例:


  • $ 5,99

  • 0,79€

I可以轻松使用 NSNumberFormatter 来开箱即用,但由于我的布局为价值和货币符号定义了不同的风格,我发现自己在一个更手动的工作场所是必需的。

I could easily use the NSNumberFormatter to get this worked out "out of the box", but since my layout defines a different style for the value and currency symbol, I've found myself in a position where a more manual workaround is required.

有什么想法吗?

推荐答案

区域设置对象似乎没有直接提供这些信息,但数字格式化器必须知道它。您不应该直接询问(新式)数字格式化程序的格式,虽然这可能有用,然后您可以查找货币符号,¤,格式为字符串。

The locale object doesn't seem to provide this information directly, but of course the number formatter must know it. You're not supposed to ask (new-style) number formatters for their format directly, although that'll probably work, and you can then look for the currency symbol, ¤, in the format string.

可能更好的是创建一个 CFNumberFormatter ,它明确允许您查看其格式,然后检查该字符串:

Possibly better would be to create a CFNumberFormatter, which does explicitly allow you to view its format, and then inspect that string:

// NSLocale and CFLocale are toll-free bridged, so if you have an existing
// NSNumberFormatter, you can get its locale and use that instead.
CFLocaleRef usLocale = CFLocaleCreate(NULL, CFSTR("en_US"));
CFNumberFormatterRef usFormatter = CFNumberFormatterCreate(NULL, usLocale, kCFNumberFormatterCurrencyStyle);
CFLocaleRef frLocale = CFLocaleCreate(NULL, CFSTR("fr_FR"));
CFNumberFormatterRef frFormatter = CFNumberFormatterCreate(NULL, frLocale, kCFNumberFormatterCurrencyStyle);

NSString * usString = (__bridge NSString *)CFNumberFormatterGetFormat(usFormatter);
NSString * frString = (__bridge NSString *)CFNumberFormatterGetFormat(frFormatter);

NSUInteger loc = ([usString rangeOfString:@"¤"]).location;
NSLog(@"Currency marker at beginning for US? %@", (loc == 0) ? @"YES" : @"NO");
loc = ([frString rangeOfString:@"¤"]).location;
NSLog(@"Currency marker at end for FR? %@", (loc == [frString length] - 1) ? @"YES" : @"NO");

这篇关于NSLocale货币符号,显示金额值之前或之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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