问题 我要格式化货币,比如美元
解决办法 使用 NumberFormat.currencyFormat( ) 方法
讨论 不像其他语言,比如 ColdFusion,ActionScript 没有提供内建的函数格式化货币数字。 自定义类NumberFormat 包括一个 currencyFormat( )方法。currencyFormat( ) 至少需要一个参数,看下面的简单代码: 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( )的一些例子代码: 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@
特别提示:本教程属于【我爱RIA网】翻译教程,如需转载请注明出处!
|