4.6.格式化货币数字

问题
我要格式化货币,比如美元
解决办法
使用NumberFormat.currencyFormat( ) 方法
讨论
不像其他语言,比如ColdFusion,ActionScript 没有提供内建的函数格式化货币数字。自定义类NumberFormat 包括一个currencyFormat( )方法。

currencyFormat( ) 至少需要一个参数,看下面的简单代码:
+展开
-ActionScript
var styler:NumberFormat = new NumberFormat( );
trace(styler.currencyFormat(123456));

在英文操作系统上运行结果如下:
$123,456.00
和format( ) 方法类似,currencyFormat( ) 方法根据自动本地化设置进行格式化。因此,如果上面的代码在西班牙语操作系统上运行结果如下:
123.456,00
覆盖自动本地化方法和format()类似:
使用Locale 对象作为currencyFormat( )的第二个参数.
赋全局变量给Locale.slanguage 和Locale.svariant 属性
使用字符对象作为currencyFormat( )的第二个参数.
currencyFormat( ) 的字符对象比format( ) 方法中稍微不一样,它包括4个属性: group, decimal,currency, 和before。group 和decimal 属性和format( ) 方法一样。currency 属性为货币符号,before 为布尔值,表示货币符号的位置。
下面是currencyFormat( )的一些例子代码:
+展开
-ActionScript
var styler:NumberFormat = new NumberFormat( );
trace(styler.currencyFormat(123456));
Locale.slanguage = "nl";
trace(styler.currencyFormat(123456));
trace(styler.currencyFormat(123456, new Locale("sv")));
trace(styler.currencyFormat(123456, {group: ",", decimal: ".", currency: "@", before: false}));

输出结果:
$123,456.00
123.456,00
123,456.00kr
123,456.00@

加支付宝好友偷能量挖...


评论(0)网络
阅读(110)喜欢(0)flash/flex/fcs/AIR