DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Currency Formatted String To Number

02.13.2008
| 4892 views |
  • submit to reddit
        Makes a currency-formatted string numeric
function currency2float(m){
    var pattern  = /[\.,][-|–]?$/;           /* 100.-- >>> 100 */
                                             /* 100,-  >>> 100 */
    var m = m.replace(pattern, "");
    
    var pattern  = /[\.,]([\d]{0,2})$/;      /* 100,95 >>> 100X95 */
    var m = m.replace(pattern, "X$1");
    
    var pattern  = /[\.',]/g;                /* delete ' , .  */
    var m = m.replace(pattern, "");
        
    var m = m.replace(/X/g, '.');            /* 100X95 >> 100.95 */
    
    if(isNaN(m*1)) return false;
    return(m*1);
}