Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Currency Formatted String To Number

DZone's Guide to

Currency Formatted String To Number

·
Free Resource
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);
}
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}