vinita jain
vinita jain

Reputation: 183

Angular - how to convert a number to float value?

I want to send a floating number as 14.00 in query, I tried multiple ways to convert the value into a floating number but it takes value as 14 only.

Different tried ways:

public convertValue(value: any) :  number{
  console.log("TRY 1!: ", parseFloat(value));// log value 14
  console.log("TRY 2!: ", parseFloat(parseFloat(value).toFixed(2)));// log value 14
  console.log("TRY 3!: ", Number.parseFloat(value));//log value 14
  console.log("TRY 4!: ", Math.round(value*Math.pow(10, 2)) / Math.pow(10, 2)); //log value 14
  console.log("TRY 6!: ",value.toPrecision(5))// throws an error precision is not a value
  return  Number(parseFloat(value).toFixed(2))
  //return parseFloat(value).toFixed(2)// can't use this because it returns string

}

Upvotes: 1

Views: 1780

Answers (1)

Ploppy
Ploppy

Reputation: 15353

14.00 is not considered a number, it is the string representation of 14.

I don't think you can achieve what you are looking for without parsing it to string.

Upvotes: 1

Related Questions