本文介绍了iOS swift NumberFormatter十进制样式本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 NumberFormatter.Style.decimal
我需要20000.23应该是20,000.23,因为我创建了一个扩展
var NumerWithDecimalPoint:String {
if self!={
let numberFormatter = NumberFormatter()
if IS_PERSIAN {
numberFormatter.locale = Locale(标识符:fa_IR)
} else {
numberFormatter.locale = Locale(标识符:EN)
}
numberFormatter.numberStyle = NumberFormatter.Style.decimal
numberFormatter.maximumFractionDigits = 2
numberFormatter.roundingMode = .floor
let number = numberFormatter.number(from:self)
let str = numberFormatter.string(from:number!)
return str!
}其他{
返回
}
}
如果在en中工作正常但在fa_IR中崩溃让number = numberFormatter.number(来自:self)
让Formatter:NumberFormatter = NumberFormatter()
Formatter.locale = Locale(标识符:EN)
let number = Formatter.number(from:self)
let str = numberFormatter.string(from:number!)
I am working with NumberFormatter.Style.decimal
I need 20000.23 should be 20,000.23 for that I create an extension
var NumerWithDecimalPoint : String{
if self != ""{
let numberFormatter = NumberFormatter()
if IS_PERSIAN{
numberFormatter.locale = Locale(identifier: "fa_IR")
}else{
numberFormatter.locale = Locale(identifier: "EN")
}
numberFormatter.numberStyle = NumberFormatter.Style.decimal
numberFormatter.maximumFractionDigits = 2
numberFormatter.roundingMode = .floor
let number = numberFormatter.number(from: self)
let str = numberFormatter.string(from: number!)
return str!
}else{
return ""
}
}
If work fine in en but crash in fa_IR let number = numberFormatter.number(from: self)
解决方案
Try this :
let Formatter: NumberFormatter = NumberFormatter()
Formatter.locale = Locale(identifier: "EN")
let number = Formatter.number(from: self)
let str = numberFormatter.string(from: number! )
这篇关于iOS swift NumberFormatter十进制样式本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!