0

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?

malifa
  • 8,025
  • 2
  • 42
  • 57
gauri
  • 309
  • 2
  • 6
  • 14
  • why would you store an "amount" value as a string in the first place? anyway, question is not about angular. – malifa Dec 13 '19 at 13:33

2 Answers2

-1

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(',', '.'));
Andrei
  • 10,117
  • 13
  • 21
-1

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(',','.'))
  }

}

Check this stackblitz link

Kazi
  • 1,461
  • 3
  • 19
  • 47