<legend id='xoEfB'><style id='xoEfB'><dir id='xoEfB'><q id='xoEfB'></q></dir></style></legend>
      • <bdo id='xoEfB'></bdo><ul id='xoEfB'></ul>

        <i id='xoEfB'><tr id='xoEfB'><dt id='xoEfB'><q id='xoEfB'><span id='xoEfB'><b id='xoEfB'><form id='xoEfB'><ins id='xoEfB'></ins><ul id='xoEfB'></ul><sub id='xoEfB'></sub></form><legend id='xoEfB'></legend><bdo id='xoEfB'><pre id='xoEfB'><center id='xoEfB'></center></pre></bdo></b><th id='xoEfB'></th></span></q></dt></tr></i><div id='xoEfB'><tfoot id='xoEfB'></tfoot><dl id='xoEfB'><fieldset id='xoEfB'></fieldset></dl></div>
      1. <small id='xoEfB'></small><noframes id='xoEfB'>

        <tfoot id='xoEfB'></tfoot>
      2. NumberFormatter 的 generateDecimalNumbers 不起作用

        generatesDecimalNumbers for NumberFormatter does not work(NumberFormatter 的 generateDecimalNumbers 不起作用)

          <legend id='MYVCD'><style id='MYVCD'><dir id='MYVCD'><q id='MYVCD'></q></dir></style></legend>
          <tfoot id='MYVCD'></tfoot>

                <i id='MYVCD'><tr id='MYVCD'><dt id='MYVCD'><q id='MYVCD'><span id='MYVCD'><b id='MYVCD'><form id='MYVCD'><ins id='MYVCD'></ins><ul id='MYVCD'></ul><sub id='MYVCD'></sub></form><legend id='MYVCD'></legend><bdo id='MYVCD'><pre id='MYVCD'><center id='MYVCD'></center></pre></bdo></b><th id='MYVCD'></th></span></q></dt></tr></i><div id='MYVCD'><tfoot id='MYVCD'></tfoot><dl id='MYVCD'><fieldset id='MYVCD'></fieldset></dl></div>
              • <small id='MYVCD'></small><noframes id='MYVCD'>

                  <tbody id='MYVCD'></tbody>
                  <bdo id='MYVCD'></bdo><ul id='MYVCD'></ul>
                  本文介绍了NumberFormatter 的 generateDecimalNumbers 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我的函数是将字符串转换为十进制

                  My function is converting a string to Decimal

                  func getDecimalFromString(_ strValue: String) -> NSDecimalNumber {
                      let formatter = NumberFormatter()
                      formatter.maximumFractionDigits = 1
                      formatter.generatesDecimalNumbers = true
                      return formatter.number(from: strValue) as? NSDecimalNumber ?? 0
                  }
                  

                  但它没有按预期工作.有时它会像这样返回

                  But it is not working as per expectation. Sometimes it's returning like

                  Optional(8.300000000000001)
                  Optional(8.199999999999999)
                  

                  而不是 8.3 或 8.2.在字符串中,我有类似8.3"或8.2"的值,但转换后的小数不符合我的要求.有什么建议我犯错了吗?

                  instead of 8.3 or 8.2. In the string, I have value like "8.3" or "8.2" but the converted decimal is not as per my requirements. Any suggestion where I made mistake?

                  推荐答案

                  generatesDecimalNumbers 设置为 true 并不像预期的那样工作.返回的值是 NSDecimalNumber 的一个实例(可以准确地表示值 8.3),但显然格式化程序首先将字符串转换为二进制浮点数(并且可以准确地表示 8.3).因此返回的十进制值只是大致正确.

                  Setting generatesDecimalNumbers to true does not work as one might expect. The returned value is an instance of NSDecimalNumber (which can represent the value 8.3 exactly), but apparently the formatter converts the string to a binary floating number first (and that can not represent 8.3 exactly). Therefore the returned decimal value is only approximately correct.

                  这也被报告为一个错误:

                  That has also been reported as a bug:

                  • NSDecimalNumbers from NSNumberFormatter 受二进制逼近影响错误
                  • NSDecimalNumbers from NSNumberFormatter are affected by binary approximation error

                  还要注意(与文档相反),maximumFractionDigits 属性在 解析 字符串时不起作用变成一个数字.

                  Note also that (contrary to the documentation), the maximumFractionDigits property has no effect when parsing a string into a number.

                  有一个简单的解决方案:使用

                  There is a simple solution: Use

                  NSDecimalNumber(string: strValue) // or
                  NSDecimalNumber(string: strValue, locale: Locale.current)
                  

                  相反,取决于字符串是否本地化.

                  instead, depending on whether the string is localized or not.

                  或者使用 Swift 3 Decimal 类型:

                  Or with the Swift 3 Decimal type:

                  Decimal(string: strValue) // or
                  Decimal(string: strValue, locale: .current)
                  

                  例子:

                  if let d = Decimal(string: "8.2") {
                      print(d) // 8.2
                  }
                  

                  这篇关于NumberFormatter 的 generateDecimalNumbers 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  iOS: How to run a function after Device has Rotated (Swift)(iOS:设备旋转后如何运行函数(Swift))
                  How can you rotate text for UIButton and UILabel in Swift?(如何在 Swift 中旋转 UIButton 和 UILabel 的文本?)
                  iOS 8+ framework with nested embedded framework(带有嵌套嵌入式框架的 iOS 8+ 框架)
                  Import Framework in Swift Project, Xcode(Swift 项目、Xcode 中的导入框架)
                  Signing embedded framework not working in Xcode 11.2.1(签署嵌入式框架在 Xcode 11.2.1 中不起作用)
                  Xcode 6.1: file was built for x86_64 which is not the architecture being linked (i386)(Xcode 6.1:文件是为 x86_64 构建的,它不是被链接的架构 (i386))

                  <tfoot id='FUmxu'></tfoot>
                    <tbody id='FUmxu'></tbody>

                  <small id='FUmxu'></small><noframes id='FUmxu'>

                  • <bdo id='FUmxu'></bdo><ul id='FUmxu'></ul>
                      <i id='FUmxu'><tr id='FUmxu'><dt id='FUmxu'><q id='FUmxu'><span id='FUmxu'><b id='FUmxu'><form id='FUmxu'><ins id='FUmxu'></ins><ul id='FUmxu'></ul><sub id='FUmxu'></sub></form><legend id='FUmxu'></legend><bdo id='FUmxu'><pre id='FUmxu'><center id='FUmxu'></center></pre></bdo></b><th id='FUmxu'></th></span></q></dt></tr></i><div id='FUmxu'><tfoot id='FUmxu'></tfoot><dl id='FUmxu'><fieldset id='FUmxu'></fieldset></dl></div>

                            <legend id='FUmxu'><style id='FUmxu'><dir id='FUmxu'><q id='FUmxu'></q></dir></style></legend>