【问题标题】:Javascript: Currency converterJavascript:货币转换器
【发布时间】:2013-08-09 12:47:01
【问题描述】:

我是 javascript 新手,我正在尝试制作一个简单的货币转换器,当我选择 "£Pound" "£Pound" 或 "£Pound" "R$Real" 但当我选择 "R $Real" "R$Real" 运行 "Pound" "R$Real" 计算。

我花了几个小时试图弄清楚这一点(非常令人沮丧)。

如何解决?还有另一种方法吗? (也尝试使用“ if ”和“ else if ”同样的问题)。谢谢!

这是 HTML

<label>Amount:</label>
<input type="text" id="amount" />
<label>From:</label>
<select id="select1">
    <option value="pound">&pound; Pound</option>
    <option value="real">R$ Real</option>
</select>
<label>To:</label>
<select id="select2">
    <option value="pound">&pound; Pound</option>
    <option value="real">R$ Real</option>
</select>
<input type="button" onClick="calculation()" value="calculate" />
<div id="result"></div>

这是 JS

function calculation() {
var amount = document.getElementById('amount').value;
var currency1 = document.getElementById('select1').value;
var currency2 = document.getElementById('select2').value;

switch((currency1)&&(currency2)){
    case "pound":
    case "pound":
        var y = amount * 1;
        document.getElementById('result').innerHTML = "&pound; " + parseFloat(Math.round(y * 100) / 100).toFixed(2);
        break
    case "pound":
    case "real":
        var x = currency2 = 3.40;
        var y = amount * x;
        document.getElementById('result').innerHTML = "R$ " + parseFloat(Math.round(y * 100) / 100).toFixed(2);
        break
    case "real":
    case "real":
        var y = amount * 1;
        document.getElementById('result').innerHTML = "R$ " + parseFloat(Math.round(y * 100) / 100).toFixed(2);
        break
    case "real":
    case "pound":
        var x = currency2 = 3.40;
        var y = amount / x;
        document.getElementById('result').innerHTML = "&pound; " + parseFloat(Math.round(y * 100) / 100).toFixed(2);
}}

【问题讨论】:

    标签: javascript converter currency


    【解决方案1】:

    要修复您的 JS,请执行以下操作:

    问题是您的 switch 将计算为单个字符串,并且您使用的是贯穿 switch 语句 jsFiddle to demonstrate what I mean

    Switch Statement Documentation for JavaScript

    function calculation() {
        var amount = document.getElementById('amount').value;
        var currency1 = document.getElementById('select1').value;
        var currency2 = document.getElementById('select2').value;
    
        switch (currency1 + ' ' + currency2) {
            case "pound pound":
                var y = amount * 1;
                document.getElementById('result').innerHTML = "&pound; " + parseFloat(Math.round(y * 100) / 100).toFixed(2);
                break
            case "pound real":
                var x = currency2 = 3.40;
                var y = amount * x;
                document.getElementById('result').innerHTML = "R$ " + parseFloat(Math.round(y * 100) / 100).toFixed(2);
                break
            case "real real":
                var y = amount * 1;
                document.getElementById('result').innerHTML = "R$ " + parseFloat(Math.round(y * 100) / 100).toFixed(2);
                break
            case "real pound":
                var x = currency2 = 3.40;
                var y = amount / x;
                document.getElementById('result').innerHTML = "&pound; " + parseFloat(Math.round(y * 100) / 100).toFixed(2);
        }
    }
    

    使用下面显示数字,然后将符号放在前面,因为此代码将在正确的位置添加逗号和分隔符,包括否定。

    将数字格式化为货币:

    function formatCurrency(num, precision) {
      //identify '(123)' as a negative number
      if (typeof num == 'string' && num.toString().indexOf('\\(') >= 0) {
        num = '-' + num;
      }
    
      if (num === '' || (num === '-' && precision === -1)) {
        return;
      }
    
      // if the number is valid use it, otherwise clean it
      if (isNaN(num)) {
        // clean number
        if (num === '' || (num === '-' && precision === -1)) {
          return;
        }
    
        if (isNaN(num)) {
          num = '0';
        }
      }
    
      // evalutate number input
      var numParts = String(num).split('.');
      var isPositive = (num == Math.abs(num));
      var hasDecimals = (numParts.length > 1);
      var decimals = (hasDecimals ? numParts[1].toString() : '0');
      var originalDecimals = decimals;
    
      // format number
      num = Math.abs(numParts[0]);
      num = isNaN(num) ? 0 : num;
      if (precision >= 0) {
        decimals = parseFloat('1.' + decimals); // prepend "0."; (IE does NOT round 0.50.toFixed(0) up, but (1+0.50).toFixed(0)-1
        decimals = decimals.toFixed(precision); // round
        if (decimals.substring(0, 1) == '2') {
          num = Number(num) + 1;
        }
        decimals = decimals.substring(2); // remove "0."
      }
      num = String(num);
    
      for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
        num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
      }
    
      if ((hasDecimals && precision == -1) || precision > 0) {
        num += '.' + decimals;
      }
    
      // format symbol/negative
      var format = isPositive ? '%s%n' : '(%s%n)';
      var money = format.replace(/%s/g, '$');
      money = money.replace(/%n/g, num);
      return money;
    }
    
    console.log(formatCurrency(12002031203120, 2))

    【讨论】:

    • (格式数字到货币:代码)这个代码是什么?你是第一个很好地获得欲望输出。
    • @Hadisurrahman 它将第一个数字格式化为金钱和小数点后的数字...随意运行上面的代码
    猜你喜欢
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多