2

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
4

1 に答える 1

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
 */

最終的には、プレーヤーが何らかの方法で基盤となる OS を使用してフォーマットを達成すると述べています。これは確かに奇妙な問題ですが、これが以前に発見されたことがなく、説明が投稿されていないか、少なくともバグレポートが実際に Flash Player の問題である場合は驚くでしょう。SDK のバージョンとプレーヤーのバージョンに関する詳細が役立つ場合があります。また、これらのいずれかをいじってみましたが、結果に変化はありますか?

また、達成しようとしていることに応じて、これに対処する必要があるのが 1 回限りのシナリオである場合、このケースの書式設定を処理する独自のクラスを作成することで、問題を回避できる場合があります。 NumberFormatter クラスのより多くの機能を必要とするのは、システム全体で使用されるものであり、根本的な問題を解決したいということを理解できました。

于 2012-05-24T20:39:20.927 に答える