![](/img/trans.png)
[英]How to make Double number to add thousand mark and set minimumFractionDigits?
[英]Format double number without thousand separator?
有誰知道將雙精度值格式化為不帶千位分隔符的字符串的方法(在Swift-iOS中)。
注意:我需要考慮當前的語言環境,因為某些歐洲語言使用逗號(,)作為小數點分隔符。 例如,德國人languange將12.50寫為12,50。
因此,如果double值為1250.25
English-uk : 1250.25
German : 1250,25
如果我使用以下兩種方法(英國英語,Whn),則得到的值為(1,212.50)。 但是我想將值格式化為不帶分隔符的字符串。 所以在這種情況下1212.50
var testOne = String.localizedStringWithFormat("%.2f", 1212.50)
var testTwo = String(format: "%.2f", locale: NSLocale.currentLocale(), 1212.50)
您需要在數字上使用NSNumberFormatter,並將分組分隔符明確設置為空字符串:
let formatter = NSNumberFormatter()
formatter.locale = NSLocale(localeIdentifier: "en_US") // locale determines the decimal point (. or ,); English locale has "."
formatter.groupingSeparator = "" // you will never get thousands separator as output
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2
formatter.numberStyle = .DecimalStyle
let result = formatter.stringFromNumber(1233.99) // you will always get string 1233.99
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.