![](/img/trans.png)
[英]my currency converter question is i a need function to convert rand(ZAR) to pound, dollar, and euro
[英]I'm trying to build a simple currency converter. my function to convert the currency keeps giving me an error
我收到此錯誤“算術運算的右側必須是“任何”、“數字”、“大整數”或枚舉類型。
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 {}
}
任一變化rate = '1.37';
rate = 1.37;
或更改return this.amount * this.rate;
return this.amount * Number(this.rate);
您還應該將變量from
重命名為convertFrom
,因為from
是保留術語。
你寫了
rate = ‚1.37‘
這被解釋為字符串。 寫成
rate=1.37
然后它應該工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.