2012-05-24 69 views
2

我碰到這種怪異的行爲來,而在使用AS3的NumberFormatter。奇怪的NumberFormatter行爲,AS3

理解我需要使用NumberBaseRoundType.UP是很重要的,因爲我需要在申請信用時使用NumberBaseRoundType.DOWN

考慮到具有爲2的值取整精度,我猜的NumberFormatter不應該改變我的電話號碼。在下面的例子中,2.43一次格式化成2.44 ..!

所以Math.pow(...)是不是我要找的PLUS解決方案,我很有興趣瞭解什麼和爲什麼發生這種情況。謝謝!

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 

回答

0

這不是一個完整的答案。但是要開始使用哪個NumberFormatter類(它來自哪個包)。某些源代碼可用,其中一些源代碼包含在播放器本身中,並且無法訪問,但從4.6框架中查看Spark中包含的內容時,它包含以下級別註釋,可以提供一些見解:

/** 
* 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>&lt;s:NumberFormatter&gt;</code> tag inherits all of the tag 
* attributes of its superclass and adds the following tag attributes:</p> 
* 
* <pre> 
* &lt;s:NumberFormatter 
* <strong>Properties</strong> 
* negativeNumberFormat="<i>locale and OS dependent</i>" 
* /&gt; 
* </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 
*/ 

雖然最終它指出,玩家使用底層的操作系統以某種方式實現的格式。這當然是一個奇怪的問題,但我會感到驚訝,如果這不是以前捕獲和解釋尚未發佈,或者至少,如果它其實是在Flash播放器的問題的bug報告。有關SDK版本和播放器版本的更多詳細信息可能會有所幫助,您是否還嘗試過擺弄其中的任何一種結果?

同樣取決於你試圖達到的目標,你可以通過編寫你自己的類來處理這個案例的格式來解決問題,如果它只是一個你需要處理的場景有了這個,如果它更像是一個系統廣泛使用的東西,需要更多的NumberFormatter類的功能,我可以理解想要解決潛在的問題。

+0

我相信他在使用MX NumberFormatter,因爲Spark沒有定義'rounding'或'precision'屬性。 –

+0

嗨,我正在使用FB 4.0.1包含在SDK 4.1.0中的mx.formatters.NumberFormatter類。當然,我將不得不寫我自己的班,因爲肯定會計系統不能依賴計算機語言環境..我在加拿大,但我總是使用美國設置,以避免任何問題..現在,我沒有任何線索..但我會嘗試進行一些測試並回復你們。謝謝!! – Jivago