简体   繁体   English

Swift:使用本地化的百万和十亿文本格式化货币

[英]Swift: Formatting currencies with localized million and billion text

I'm working on an app that shows currencies with large numbers.我正在开发一个显示大数字货币的应用程序。 To make it more user friendly it would be nice to show the numbers with letters, eg:为了使其更加用户友好,最好用字母显示数字,例如:

$24,514,983,671 -> $24.5B // English $24,514,983,671 -> $24.5B // 英文
3,306,777.10 € -> 3,31 Mio. 3,306,777.10 € -> 3,31 Mio。 € // German € // 德语
kr 23 000 000 -> kr 23 mill. kr 23 000 000 -> kr 23 磨。 // Norwegian // 挪威语

The numbers should have minimumSignificantDigits = 1 and maximumSignificantDigits = 3.这些数字应该有 minimumSignificantDigits = 1 和 maximumSignificantDigits = 3。

How should this be solved in Swift?这应该如何在 Swift 中解决? Is there any library available?有可用的图书馆吗?

Android has this solution 安卓有这个解决方案

It seems to be based on swift-corelibs-foundation: Github , but I can't see how to use it in my app.它似乎基于 swift-corelibs-foundation: Github ,但我看不到如何在我的应用程序中使用它。

Will I have to make the logic myself with localized translations?我是否必须自己使用本地化翻译来制作逻辑? I have found an answer for using general abbreviations K/M/B for large numbers here: iOS convert large numbers to smaller format , but it does not solve the whole problem.我在这里找到了对大数使用通用缩写 K/M/B 的答案: iOS convert large numbers to small format ,但它并没有解决整个问题。

You will have to implement your own solution, but it is not hard.您将不得不实施自己的解决方案,但这并不难。 One way to go would be to create two dictionaries where the key will be the Locale identifier and the value the translation:一种方法是创建两个字典,其中键是区域设置标识符,值是翻译:

let millionTrans = [ "en_US" : "million", ...]
let billionTrans = [ "en_US': "billion", ...]

then get the Locale.current , find out if the amount is in the millions or billions and query the appropriate dictionary to get its value.然后获取Locale.current ,找出金额是数百万还是数十亿并查询相应的字典以获取其值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM