Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用javascript编写货币转换器?_Javascript - Fatal编程技术网

如何用javascript编写货币转换器?

如何用javascript编写货币转换器?,javascript,Javascript,如何搜索文本中包含欧元或欧元或其他货币的所有部分,以将其转换为我选择的其他货币 即: 如何区分欧元或欧元或其他货币符号的左边或右边的数字 如何用更改的值替换该数字 我如何将此应用于网页上所有在前后有欧元或欧元或其他货币符号的数字 在文本中根本无法确定是否真的使用了某种货币 例如,您如何区分这些: 欧元很好 我们需要50欧元(欧元) 不同于50欧元 我也是$ 可以像这样使用50美元 或者像这样50美元 甚至像这样50美元(由欧洲人) 但我会让你处理这部分 如果您有一个字符串,您知道它是一种货币

如何搜索文本中包含欧元或欧元或其他货币的所有部分,以将其转换为我选择的其他货币

即:

  • 如何区分欧元或欧元或其他货币符号的左边或右边的数字
  • 如何用更改的值替换该数字
  • 我如何将此应用于网页上所有在前后有欧元或欧元或其他货币符号的数字

  • 在文本中根本无法确定是否真的使用了某种货币

    例如,您如何区分这些:

    • 欧元很好
    • 我们需要50欧元(欧元)
    • 不同于50欧元
    • 我也是$
    • 可以像这样使用50美元
    • 或者像这样50美元
    • 甚至像这样50美元(由欧洲人)
    但我会让你处理这部分

    如果您有一个字符串,您知道它是一种货币,您可以这样玩:

    // Find out what's the currency used
    if ( /[$]/.test( str ) ) // Currency is $
    else if ( /[€]/.test( str ) ) // Currency is €
    
    // etc. To get the number out of this string, use:
    var val = /\d+/.exec( str ); // if "str === 50$", it returns "50"
    

    以下是一些示例代码,用于在文本中搜索任何货币并转换为目标货币:

    var currencies = {
        "EUR": ["€", "EUR", "EURO", "EUROS"],
        "USD": ["$", "USD", "USDOLLAR", "USDOLLARS"],
        "DEM": ["DEM", "DM", "Deutsche Mark"]
    };
    var rates = {
        "EUR": { USD: 1.2613, DEM: 1.96 },
        "USD": { EUR: 0.792832792, DEM: 1.54315 },
        "DEM": { USD: 0.648121, EUR: 0.51 }
    };
    function currencyReplacer(number, sourceCurrency, targetCurrency, currencyFormatIndex, currencySeparator) {
        var c, i, comma = /,/g;
        for (c in currencies) {
            if (currencies.hasOwnProperty(c)) {
                for (i = 0; i < currencies[c].length; i++) {
                    if (currencies[c][i] === sourceCurrency) {
                        console.log(rates[c][targetCurrency], number);
                        return [Math.round(rates[c][targetCurrency] * number.replace(comma, "."), 2), currencies[targetCurrency][currencyFormatIndex || 0]].join(currencySeparator || "");
                    }
                }
            }
        }
        return m;
    }
    function replaceCurrencies(text, sourceCurrency, targetCurrency, currencyFormatIndex, currencySeparator) {
        var prefixedRegex = new RegExp("(" + currencies[sourceCurrency].join("|") + ")\\s?(\\d+(?:(?:,|.)\\d+)?)", "gi");
        var suffixedRegex = new RegExp("(\\d+(?:(?:,|.)\\d+)?)\\s?(" + currencies[sourceCurrency].join("|") + ")", "gi");
        return text.replace(prefixedRegex, function(m, currency, number) {
            return currencyReplacer(number, currency, targetCurrency, currencyFormatIndex, currencySeparator);
        }).replace(suffixedRegex, function(m, number, currency) {
            return currencyReplacer(number, currency, targetCurrency, currencyFormatIndex, currencySeparator);
        });
    }
    replaceCurrencies("This function will convert currencies: €50 is less than 100 EUR which is more than 75 €", "EUR", "DEM", 1, " ");
    // will output: "This function will convert currencies: 98 DM is less than 196 DM which is more than 147 DM"
    
    var货币={
    “欧元”:[“欧元”、“欧元”、“欧元”、“欧元”],
    “USD”:[“$”、“USD”、“USDOLLAR”、“USDOLLAR”],
    “德国马克”:[“德国马克”、“德国马克”、“德国马克”]
    };
    风险值比率={
    “欧元”:{美元:1.2613,德国马克:1.96},
    “美元”:{欧元:0.792832792,德国马克:1.54315},
    “德国马克”:{美元:0.648121欧元:0.51}
    };
    函数currencyReplacer(数字、源货币、目标货币、currencyFormatIndex、currencySeparator){
    变量c,i,逗号=/,/g;
    (c)货币{
    国际单项体育联合会(货币、自有财产(c)){
    对于(i=0;i<货币[c]。长度;i++){
    if(货币[c][i]==来源货币){
    console.log(汇率[c][targetCurrency],编号);
    返回[Math.round(rates[c][targetCurrency]*number.replace(逗号,“.”,2),货币[targetCurrency][currencyFormatIndex | | | 0]]。加入(currencySeparator | | |“);
    }
    }
    }
    }
    返回m;
    }
    函数替换货币(文本、源货币、目标货币、currencyFormatIndex、currencySeparator){
    var prefixedRegex=new RegExp(“(”+货币[sourceCurrency])。加入(“|”+”)\\s(\\d+(?:(?:,|。)\\d+),“gi”);
    var-suffexedregex=new RegExp(“(\\d+)(?:(?:,|])\\d+)\\s”(“+货币[sourceCurrency]。加入(“|”)+”,“gi”);
    返回文本.replace(前缀dregex,函数(m,货币,数字){
    返回currencyReplacer(数字、货币、目标货币、currencyFormatIndex、currencySeparator);
    }).replace(后缀,函数(m,数字,货币){
    返回currencyReplacer(数字、货币、目标货币、currencyFormatIndex、currencySeparator);
    });
    }
    替换货币(“此功能将转换货币:50欧元小于100欧元,大于75欧元”,“欧元”,“德国马克”,“1”);
    //将输出:“此函数将转换货币:98德国马克小于196德国马克,大于147德国马克”
    
    这能解决你的问题吗

    编辑:更新了上面的代码,以包含您想要的目标货币DEM,并使regex/replace同时支持前缀和后缀,还支持sourceCurrency


    Edit2:再次更新代码以处理十进制数

    我认为更好的方法是为用户提供下拉列表以选择货币,并将标称字段保留为整数。但是你的电话(或你的客户的)无论如何:)请原谅我的无意义测试,好吧,所以我在这里包括了你的版本:它没有替换值…我在代码中提供的正则表达式只有在欧元数在欧元符号前面时才匹配。如果需要匹配这样键入的实例:€50,请改用此正则表达式:
    var regex=/(€?EURO?S?)\S*(\d+)/gi如果您需要同时匹配50欧元和50欧元,则必须重写代码以支持这两个正则表达式/案例。是的,我需要50欧元和50欧元和50欧元,50欧元和50欧元,50欧元和50欧元和50欧元确保费率是最新的。它们的格式应为
    /*one*/EUR:{/*is*/DEM:1.96}