I'm following this tutorial: https://www.tutorialesprogramacionya.com/angularya/detalleconcepto.php?punto=89&codigo=89&inicio=80. and it has the following part of code:
import { Component } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
conversion!: string;
potenciaNumero!: number;
cantidad!: number;
formularioConversion = new FormGroup({
numerodecimal: new FormControl(''),
base: new FormControl('octal'),
potencia: new FormControl('2'),
largo: new FormControl(true)
});
submit() {
if (this.formularioConversion.value.base == "hexadecimal")
this.conversion = parseInt(this.formularioConversion.value.numerodecimal).toString(16);
if (this.formularioConversion.value.base == "octal")
this.conversion = parseInt(this.formularioConversion.value.numerodecimal).toString(8);
this.potenciaNumero = Math.pow(parseInt(this.formularioConversion.value.numerodecimal?), parseInt(this.formularioConversion.value.potencia));
if (this.formularioConversion.value.largo)
this.cantidad = this.formularioConversion.value.numerodecimal.length;
}
}
the problem is that in the submit function i have the variable "numerodecimal" that is defined as an object but threated as a string. How can i solve this error?
You can use typecasting to get over this. Here's some options in order of preference
parseInt(this.formulatrioConversion.value.numerodecimal || "")
parseInt(this.formulatrioConversion.value.numerodecimal as string)
parseInt(this.formulatrioConversion.value.numerodecimal as any)