在AS3中使用NumberFormatter时,我遇到了这种奇怪的行为。
重要的是要理解,我需要使用NumberBaseRoundType.UP,因为我在申请信用时需要使用NumberBaseRoundType.DOWN。
考虑到值为2的舍入精度,我想NumberFormatter不应该更改我的数字。在下面的示例中,2.43一旦格式化就变成了2.44..!
所以Math.pow(...)不是我正在寻找的解决方案,另外,我非常有兴趣了解发生这种情况的原因和原因。谢谢!
var roundUp:NumberFormatter = new NumberFormatter();
roundUp.rounding = NumberBaseRoundType.UP;
roundUp.precision = 2;
trace(roundUp.format(2.41)); // Output : 2.41
trace(roundUp.format(2.42)); // Output : 2.42
trace(roundUp.format(2.43)); // Output : 2.44 <-- ???
trace(roundUp.format(2.44)); // Output : 2.44
trace(roundUp.format(2.45)); // Output : 2.46 <-- ???
trace(roundUp.format(2.46)); // Output : 2.46发布于 2012-05-25 04:39:21
这不是一个完整的答案。但首先,您使用的是哪个NumberFormatter类(它来自哪个包)。有些源代码是可用的,有些则包含在播放器本身中,无法访问,但在从4.6框架查看Spark中包含的on时,它包含了这个类级别的注释,它提供了一些见解:
/**
* The NumberFormatter class provides locale-sensitive formatting
* and parsing of numeric values. It can format <code>int</code>,
* <code>uint</code>, and <code>Number</code> objects.
*
* <p>This class is a wrapper class around the
* flash.globalization.NumberFormatter class.
* Therefore, the locale-specific formatting
* is provided by the flash.globalization.NumberFormatter.
* However, this NumberFormatter class can be used in MXML declarations,
* uses the locale style for the requested Locale ID name, and has
* methods and properties that are bindable.
* </p>
*
* <p>The flash.globalization.NumberFormatter class use the
* underlying operating system for the formatting functionality and
* to supply the locale-specific data. On some operating systems, the
* flash.globalization classes are unsupported, on these systems this wrapper
* class provides fallback functionality.</p>
*
* @mxml <p>The <code><s:NumberFormatter></code> tag inherits all of the tag
* attributes of its superclass and adds the following tag attributes:</p>
*
* <pre>
* <s:NumberFormatter
* <strong>Properties</strong>
* negativeNumberFormat="<i>locale and OS dependent</i>"
* />
* </pre>
*
* @includeExample examples/NumberFormatterExample1.mxml
* @includeExample examples/NumberFormatterExample2.mxml
*
* @see flash.globalization.NumberFormatter
*
* @langversion 3.0
* @playerversion Flash 10.1
* @playerversion AIR 2.5
* @productversion Flex 4.5
*/最终,它声明播放器以某种方式使用底层操作系统来实现格式化。这当然是一个奇怪的问题,但如果之前没有发现这个问题,并且没有发布解释,或者至少是一个bug报告,如果它实际上是flash player中的一个问题,我会感到惊讶。关于你的SDK版本和播放器版本的更多细节可能会有所帮助,你也尝试过修改这两个版本中的任何一个吗?结果有什么变化吗?
此外,根据您试图实现的功能,您可以通过编写自己的类来处理此问题,如果这只是您需要处理的一次性场景,如果它更多地是系统范围内使用的东西,需要更多的NumberFormatter类的功能,我可以理解,希望解决底层问题。
https://stackoverflow.com/questions/10743699
复制相似问题