我有一个文本框,其中将有一个货币字符串,然后我需要将该字符串转换为double来执行一些操作。

$1,100.00→1100.00

这需要发生在所有客户端。我别无选择,只能将货币字符串作为货币字符串作为输入,但需要将其强制转换/转换为double以允许一些数学操作。


当前回答

这个例子运行正常

Var货币= "$1,123,456.00"; var =数量数量(currency.replace (/ [^ 0 - 9 \] + / g, " ")); console.log(数量);

其他回答

let thousands_seps = '.';
let decimal_sep = ',';

let sanitizeValue = "R$ 2.530,55".replace(thousands_seps,'')
                         .replace(decimal_sep,'.')
                         .replace(/[^0-9.-]+/, '');

// Converting to float
// Result 2530.55
let stringToFloat = parseFloat(sanitizeValue);


// Formatting for currency: "R$ 2.530,55"
// BRL in this case
let floatTocurrency = Number(stringToFloat).toLocaleString('pt-BR', {style: 'currency', currency: 'BRL'});

// Output
console.log(stringToFloat, floatTocurrency);
jQuery.preferCulture("en-IN");
var price = jQuery.format(39.00, "c");

输出为:39.00卢比

use jquery.glob.js,
    jQuery.glob.all.js

// "10.000.500,61 TL" price_to_number => 10000500.61

// "10000500.62" number_to_price => 10.000.500,62

JS fiddle.net/limitlessisa/oxhgd32c/

var price="10.000.500,61 TL";
document.getElementById("demo1").innerHTML = price_to_number(price);

var numberPrice="10000500.62";
document.getElementById("demo2").innerHTML = number_to_price(numberPrice);

function price_to_number(v){
    if(!v){return 0;}
    v=v.split('.').join('');
    v=v.split(',').join('.');
    return Number(v.replace(/[^0-9.]/g, ""));
}

function number_to_price(v){
    if(v==0){return '0,00';}
    v=parseFloat(v);
    v=v.toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
    v=v.split('.').join('*').split(',').join('.').split('*').join(',');
    return v;
}

js是正确的方法。我在一个项目中使用过它,并有很好的使用经验。

accounting.formatMoney(4999.99, "€", 2, ".", ","); // €4.999,99
accounting.unformat("€ 1.000.000,00", ","); // 1000000

你可以在GitHub上找到它

简单地说

Number(currency.replace(/[^0-9-]+/g,""))/100;

适用于所有货币和地区。替换所有非数字字符(您可以有€50.000,00或$50,000.00)输入必须有2位小数