calculate and replace special numbers in pages (javascript)
I am trying to change format of special numbers in my pages.
Numbers format is similar this: 11,111.00 AUD
And these changes should be applied on them:
- Extract clean number from it: (11111)
- Calculate cost of them: (11111 * 2)
- Change currency String with USD
- Replace Previous Result with Default Value: 22222 USD
Please help me to complete this code:
$(doc开发者_StackOverflowument).ready(function (){
$('body').html( function(i,txt) {return txt.replace(/\d+,\d+.\d+ USD/,'blabla AUD'); });
});
use parseFloat('11,111.00 AUD'.replace(',',''))
method of javascript
UPDATE
return (parseFloat(txt.replace(/,/g,'')) * 2)+ " USD"
See demo of the following →
You will have to fix the formatter for this to work for return values above 999,999, but this should work pretty well for you:
var convertAUDtoUSD = function(i,txt) {
var formatter = function( val ) {
val = String(val);
return (val.length > 3) ?
val.substring(0, val.length - 3)
+ ","
+ val.substring(val.length - 3, val.length)
: val;
},
replacer = function (str, p1, p2) {
return formatter(parseInt(p1.replace(/\,/g,''),10) * 2)
+ p2.replace('AUD','USD');
};
return txt.replace(/(\d+,?\d+)(\.\d+\sAUD)/ig, replacer);
};
$('body').html( convertAUDtoUSD );
Demo →
This will strip out the commas and 'AUD', multiply the resulting value by 2 and concatenate the 'USD' text.
return txt.replace(/(,|AUD)/g,'') * 2 + " USD";
精彩评论