I want to convert a string like "20,54"
into a float like 20.54
but with parseFloat()
it is only returning 20
in this example:
this.Amount = parseFloat(order.Amount);
where order.Amount
is "20,54"
.
How can i solve this?
I want to convert a string like "20,54"
into a float like 20.54
but with parseFloat()
it is only returning 20
in this example:
this.Amount = parseFloat(order.Amount);
where order.Amount
is "20,54"
.
How can i solve this?
the reason is that valid js float format is number with point. to convert that you could try to replace comma with point
this.Amount = parseFloat(order.Amount.replace(',', '.'));
This code should solve your problem.
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<h1>the number is : {{amount}}</h1>
<button (click)="convert2Float()"> Click to Float</button>`,
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
amount: string|number = '20,54';
convert2Float() {
this.amount = parseFloat(this.amount.replace(',','.'))
}
}