Actionscript parse currency to get a number
i used a CurrencyFormatter to parse 2 number into its currency representation
currencyFormat.format("10" + "." + "99") ---> $10.99
I'm curious if there is a way to parse a string "$10.99" back to a number / double ? so it is possible to get the value on the left side of the decimal and right side of the decimal开发者_StackOverflow中文版.
thanks,
You could do this a number of ways. Here are 2 off the top of my head:
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);
精彩评论