计算器

时间:2017-02-13 06:39:16

标签: javascript string floating-point ecmascript-6

我需要专家协助计算器。这是js for angular,所以没有打字稿,但它运行并给我一个计算器。但是,百分比函数仅适用于100以下的数字。如果我执行400%的4%,则得到0.44。但如果我做100%的10%,我就会得到10。

请帮忙!

export class CalculatorComponent {


private runningTotal = 0;
private previousTotal = 0;
private currentOperator = null;


numberButtonClick(event) {


this.runningTotal = parseFloat('' + this.runningTotal + event.target.value);
  console.log(this.runningTotal)

}


changeSettings(operator) {
  this.previousTotal = this.runningTotal;
  this.runningTotal = 0;
  this.currentOperator = operator;
  console.log(this.currentOperator);
}


operativeButtonClick(event) {

    let operator = event.target.innerHTML;




    switch (operator) {
        case '=':
            this.makeCalculation();
            break;
        case '+':
            this.changeSettings(operator);
            break;
        case '-':
          this.changeSettings(operator);
            break;
        case '*':
          this.changeSettings(operator);
            break;
        case '/':
          this.changeSettings(operator);
            break;
        case '%':
            this.changeSettings(operator);
            break;
        case 'AC':
            this.runningTotal = 0;
            this.newTotal = true;
            break;
        case '.':
            this.runningTotal += ('' + '.');
            break;
    }


}



makeCalculation() {
    switch (this.currentOperator) {
        case '+':
            this.runningTotal += this.previousTotal;
            break;
        case '-':
            this.runningTotal = this.previousTotal - this.runningTotal;
            break;
        case '*':
            this.runningTotal *= this.previousTotal;
            break;
        case '/':
            this.runningTotal = this.previousTotal / this.runningTotal;
            break;
        case '%':
            this.runningTotal = ((this.previousTotal / this.runningTotal) * 100).toFixed(2);
            console.log(this.previousTotal)
            break;
    }
}

}

0 个答案:

没有答案