【问题标题】:I'm trying to build a simple currency converter. my function to convert the currency keeps giving me an error我正在尝试构建一个简单的货币转换器。我的货币兑换功能不断给我一个错误
【发布时间】:2021-01-27 12:37:29
【问题描述】:

我收到此错误“算术运算的右侧必须是“任何”、“数字”、“大整数”或枚举类型。”

import { Component, OnInit } from '@angular/core';
    
    @Component({
      selector: 'app-convert',
      templateUrl: './convert.component.html',
      styleUrls: ['./convert.component.css']
    })
    export class ConvertComponent implements OnInit {
      amount = 1;
      from = 'USD';
      to = 'GBP';
      rate = '1.37';
    
      // function for conversion.
      convert(): number {
        return this.amount * this.rate;
      }
    
        
    
      constructor() {}
    
      ngOnInit(): void {}
    }

【问题讨论】:

  • 您的汇率是一个字符串..
  • 无关,from 是 Typescript 中的保留字。您甚至之前已经使用了很多行。我会避免将其用作变量名。
  • 它甚至在这里的漂亮打印中显示为蓝色
  • 您的问题是什么?请更清楚您的问题。
  • @Daniel.Wang error 'The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enumtype.'

标签: javascript angular currency


【解决方案1】:

rate = '1.37'; 更改为rate = 1.37;
或改变 return this.amount * this.rate;return this.amount * Number(this.rate);

您还应该将变量from 重命名为convertFrom,因为from 是保留术语。

【讨论】:

    【解决方案2】:

    你写的

    rate = ‚1.37‘
    

    这被解释为一个字符串。写成

    rate=1.37 
    

    那么它应该可以工作

    【讨论】:

      猜你喜欢
      • 2023-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      相关资源
      最近更新 更多