Parse Float With Two Decimal Places In JavaScript

To parse float with two decimal places in JavaScript –

1. Call the parseFloat() function, Number() function or Unary Plus(+) operator and pass the number as an argument to the function or operator. This will give a number as a result.

2. Call the toFixed(2) method on the returned value to get a String value with float parsed to 2 places.

how to parse float with two decimal places in javascript

parseFloat() To Parse Float With Two Decimal Places In JavaScript

The parseFloat() function is used to parse a value to a String in JavaScript and return a number.

Syntax:

parseFloat(value)

Example:

parseFloat("12.5") //returns 12.5

parseFloat("12.3456") //returns 12.3456

The toFixed() method is used on a number and takes an integer as an input which represents the number of digits after the decimal point. The method rounds the number if necessary, or adds trailing zeroes upto the given precision.

Syntax:

number.toFixed(precision)

Example:

let num = 12.3456;

console.log(num.toFixed(2)); //returns 12.35

console.log(num.toFixed()); //returns 12 (default precision is 0)

So, to parse a float with 2 decimal places in JavaScript –

  1. Call the parseFloat() function and pass the number as an argument to the function.
  2. Call the toFixed(2) method on the returned value from parseFloat().

Here’s an example of how this can be done:

let num = "12.3456";

let result = parseFloat(num).toFixed(2);

console.log(result); //returns 12.35

As you can see, first we call the parseFloat() function which returns a number (12.3456). We then call the toFixed() method on this number and pass 2 as an argument which tells the method to return a value with 2 decimal places. The result of this code is 12.35 which gets logged to the console.

Note that the result is a String and if you want to get a number as a result, you will have to call the parseFloat() method on the result again.

Number() Method To Parse Float With Two Decimal Places In JavaScript

You can also use the Number() function to parse a float with two decimal places in JavaScript. The Number() function takes a value as an input and returns it as a number.

Syntax:

Number(value)

Example:

let num = "12.3456";

let result = Number(num).toFixed(2);

console.log(result); //returns 12.35

The result of this code is the same as the previous example – 12.35 gets logged to the console.

Unary Plus Operator To Parse Float With Two Decimal Places In JavaScript

Similarly, you can also use the unary plus (+) operator to convert a value to a number.

Example:

let num = "12.3456"; 

let number = +num;

console.log(number.toFixed(2)); //return 12.35

The unary plus operator is equivalent to calling the Number() function and can be used to convert a value to a number. In this example, we first convert the string “12.3456” to a number using the unary plus operator and then call the toFixed() method on it with 2 as an argument which rounds off the number to 2 decimal places. The result of this code is again 12.35 which gets logged to the console.

Note that in any of the above cases, if the returned value has a trailing 0 after the decimal point, it will be removed. For example, if you parse 12.30 with 2 decimal places, the result will be 12.3 and not 12.30.

This is because when a number is represented as a String, any trailing 0″s after the decimal point are removed in JavaScript.

Conclusion

In this article, you learned how to parse a float with 2 decimal places in JavaScript using different methods. You can use any of the above methods depending on your requirement. I hope this article was helpful and that you now have a better understanding of how to parse floats with 2 decimal places in JavaScript.

Leave a Reply