"javascript: currency converter" Code Answer

2

to fix your js do the following:

the issue is that your switch would compute to a single string, and you are using fall-through switch statements, 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 = "£ " + 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 = "£ " + parsefloat(math.round(y * 100) / 100).tofixed(2);
    }
}

use the below to display the number and then just put the symbol in front, as this code will add commas and separators in the right spots, including negative.

format number to currency:

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))
By LateCoder on June 7 2022

Answers related to “javascript: currency converter”

Only authorized users can answer the Search term. Please sign in first, or register a free account.