自定义数字格式化程序与货币符号

时间:2017-09-14 07:19:35

标签: swift macos cocoa nsnumberformatter

我有一个文本字段,用户应该输入价格。 在此文本字段中,我使用以下设置拖动了自定义数字格式化程序:

enter image description here

这几乎是完美的,但我对“格式”有疑问。 目前是:.#.##

但我必须更改以在结尾处自动显示正确的用户货币符号?

3 个答案:

答案 0 :(得分:3)

对于静态解决方案,您只需将格式从.#.##更改为.#.## €

即可

如果您正在寻找通用方法,那么您对符号¤感兴趣,后者负责货币符号。

所以你要找的格式是,#.## ¤

最后一步是检查选项lenient,否则您需要在文本字段中提供完整格式。

更多详情

自OSX 10.9和iOS 7以来,格式字符串使用来自the Unicode Technical Standard #35version 31的模式。

您可以在此版本here

中找到所有数字格式模式

Apple documentation about number formatters

答案 1 :(得分:0)

您可以使用数字格式化程序,如下所示:

let price = 592.12
let formatter = NumberFormatter()
formatter.numberStyle = .currency

//change this to get your desired currency
formatter.locale = Locale(identifier: "de_DE")
formatter.maximumFractionDigits = 0;

let priceText = formatter.string(for: price)
//priceText = 592,12 €

答案 2 :(得分:0)

您可以使用格式化程序提供的默认货币模式,还是需要执行其他自定义格式设置:

enter image description here