- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Swift 中编写了一个函数(字符串扩展)来获取任何货币字符串,例如“$500.00”或“250,75 €”,甚至“500”,然后从中返回一个 nsdecimalnumber。
不幸的是,当我传入“250,75 €”时,它命中了我代码的 if (nf.number(from: self) == nil) 部分并评估为 nil,因此什么也不返回。它似乎只在您用逗号添加更改时发生,例如 250,75。但是,如果我做了 250,它就会起作用。它似乎只为欧元这样做。美元运作良好($500.00、125.75 等)
这是我的代码。有什么想法吗?感谢您的宝贵时间!
extension String{
func toDecimalFromCurrencyString() -> NSDecimalNumber
{
let nf = NumberFormatter()
nf.numberStyle = NumberFormatter.Style.currency
nf.formatterBehavior = NumberFormatter.Behavior.behavior10_4
nf.generatesDecimalNumbers = true
var test = self.replacingOccurrences(of: nf.currencySymbol!, with: "", options: NSString.CompareOptions.literal, range: nil)
test = test.replacingOccurrences(of: nf.currencyDecimalSeparator!, with: "", options: NSString.CompareOptions.literal, range: nil)
test = test.replacingOccurrences(of: nf.currencyGroupingSeparator!, with: "", options: NSString.CompareOptions.literal, range: nil)
let scanner: Scanner = Scanner(string:test)
let isNumeric = (scanner.scanDecimal(nil) && scanner.isAtEnd)
//If the string entered isn't numeric (for example ... or $$$, then fail)
if (self.trimmingCharacters(in: CharacterSet.whitespaces).utf16.count == 0 || (!isNumeric))
{
return 0.0
}
//If the string contains more than one decimal separator (.), or more than one currency symbol ($), return 0.0
else if ((self.components(separatedBy: nf.currencyDecimalSeparator!).count-1) > 1 || (self.components(separatedBy: nf.currencySymbol!).count - 1) > 1) {
return 0.0
}
//If the string contains more than one consecutive currency grouping separator (i.e. ,,) then return 0.0
else if ((components(separatedBy: nf.currencyGroupingSeparator! + nf.currencyGroupingSeparator!).count-1) > 0)
{
return 0.0
}
else
{
if (nf.number(from: self) == nil) {
return 0.00
}
else {
return nf.number(from: self) as! NSDecimalNumber
}
}
}
}
最佳答案
您可以简单地从字符串中删除所有非数字并使用两个数字格式化程序,一个带点,另一个带逗号,并在链中使用 nil 合并运算符,如下所示:
extension Formatter {
static let decimalWithDotSeparator: NumberFormatter = {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.decimalSeparator = "."
return numberFormatter
}()
static let decimalWithCommaSeparator: NumberFormatter = {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.decimalSeparator = ","
return numberFormatter
}()
}
extension String {
var numberFromCurrency: NSDecimalNumber? {
var string = self
while let first = string.characters.first, Int("\(first)") == nil {
string = String(string.characters.dropFirst())
}
while let last = string.characters.last, Int("\(last)") == nil {
string = String(string.characters.dropLast())
}
return (Formatter.decimalWithDotSeparator.number(from: string) ??
Formatter.decimalWithCommaSeparator.number(from: string) ?? 0)
.decimalValue.decimalNumber
}
}
extension Decimal {
var decimalNumber: NSDecimalNumber { return NSDecimalNumber(decimal: self) }
}
let dollar = "$249.99"
dollar.numberFromCurrency // 249.99
let real = "R$249,99"
real.numberFromCurrency // 249.99
let euro = "0,50€"
euro.numberFromCurrency // 0.5
关于ios - NumberFormatter 和货币字符串的欧元错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43773135/
我配置了一个基本的 NumberFormatter: let numberFormatter = NumberFormatter() numberFormatter.numberStyle = .sp
我没有找到任何java问题可以帮助我解决我的问题,所以我来了。 我目前正在尝试使用带有 JFormattedTextField 的 NumberFormatter 在用户输入价格时在 GUI 中格式化
这是我的代码: extension NSDecimalNumber { func getFormattedDecimalString() -> String { let numberForma
我在 Swift 的 NumberFormatter 中发现了一个奇怪的行为。 func testNumberFormat() { let numberFormat = { (number:
在进行货币格式设置时,我在尝试设置智利比索格式时发现了一个问题。 按照此代码: let priceFormatter = NumberFormatter() priceFormatter.locale
我在 Swift 中编写了一个函数(字符串扩展)来获取任何货币字符串,例如“$500.00”或“250,75 €”,甚至“500”,然后从中返回一个 nsdecimalnumber。 不幸的是,当我传
在阅读 The Big Nerd Ranch Guide 这本书时,我发现其中一章中的一段话要求您创建 NumberFormatter 的实例。一切都按预期工作,但我注意到格式化程序是使用 closu
我正在解码我想要格式化并作为 Double 返回的小数的字符串版本: let str = "0.945404" let numberFormatter = NumberFormatter() numb
我尝试使用 intl 包中的 NumberFormatter 类将整数拼写成斯洛文尼亚语单词(用于邮政申报),但结果完全错误且毫无意义。 $fmt = new NumberFormatter('sl'
以下代码的输出是:ARS 59.00。为什么没有打印符号? let formatter = NumberFormatter() formatter.currencyCode = "ARS" forma
当尝试在我的 PHP 8.1 alpine docker 容器中格式化货币时,我总是获得默认语言环境 (en_US)。 $ docker run -it --rm php:8.1-fpm-alpine
试图从使用 PHP NumberFormatter 格式化的货币中删除最后一个 .00,但似乎不可能。我可以看到这个选项,但它似乎不影响货币:DECIMAL_ALWAYS_SHOWN $nf = ne
我正在使用 NumberFormatter 来格式化数字,我已经包含了一个 js 文件并编写了一个简单的代码,但它不起作用。我正在关注此链接http://code.google.com/p/jquer
下面是我用来格式化数字并将其重新分配回文本字段的代码。在删除文本时,我遇到一些问题,完整的文本被删除,而不是逐字符删除,而且我无法重新输入文本。请指导我解决这个问题。 let currencyForm
我为我的 amcharts serial-graph 做了以下设置。 var data = [{ "name": "Test Name 1", "count": 31, "am
使用 NumberFormatter 比简单转换有什么优势吗?例如,下面的示例 1 和 2 似乎都在做同样的工作,但强制转换似乎更容易阅读,那么为什么我发现的代码示例中经常使用 NumberForma
像这样制作我自己的格式化程序: enum Formatters { enum Number { static let moneyFormatter: NumberFormatter = {
我想按照以下规则格式化和显示 Double: If there are more than 2 fractional digits, display it as 6 significant figur
我正在使用 NumberFormatter.Style.decimal 我需要 20000.23 应该是 20,000.23 因为我创建了一个扩展 var NumerWithDecimalPoint
是否可以使用 PHP NumberFormatter类根据使用 setPattern 的数值以单数或复数形式显示单词方法? 此方法使用 ICU DecimalFormat图书馆,但我不熟悉。 当值大于
我是一名优秀的程序员,十分优秀!