我使用CurrencyFormatter将2个数字解析为其货币表示
currencyFormat.format(“10”+“。”+“99”)---> $ 10.99
我很好奇是否有办法将字符串“$ 10.99”解析回数字/ double? 所以可以得到小数点左侧和小数点右侧的值。
感谢,
答案 0 :(得分:2)
你可以通过多种方式做到这一点。这是我头顶的两个:
function currencyToNumbers($currency:String):Object {
var currencyRE:RegExp = /\$([1-9][0-9]+)\.?([0-9]{2})?/;
var val = currencyRE.exec($currency);
return {dollars:val[1], cents:val[2]};
}
function currencyToNumbers2($currency:String):Object {
var dollarSignIndex:int = $currency.indexOf('$');
if (dollarSignIndex != -1) {
$currency = $currency.substr(dollarSignIndex + 1);
}
var currencyParts = parseFloat($currency).toString().split(".");
return {dollars:currencyParts[0], cents:currencyParts[1]};
}
var currency:Object = currencyToNumbers('$199.99');
trace(currency.dollars);
trace(currency.cents);
var currency2:Object = currencyToNumbers2('$199.99');
trace(currency2.dollars);
trace(currency2.cents);