# JavaScript String To Float 2 Decimal

Last Updated : Mar 11, 2024

In this tutorial we will show you the solution of JavaScript string to float 2 decimal, as there are many methods with the help of which we can hide elements by id in JavaScript.

Here, below we are going to show a couple of links in form of code in which we understand about the conversion.

## Step By Step Guide On JavaScript String To Float 2 Decimal :-

One thing to note here that we have to convert string to float, but the float must have two decimals. Let us see the code.

```<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<body>
<h1>JavaScript string to float 2 decimal</h1>
<p>The parseFloat() Method</p>
<p>parseFloat() parses a string and returns the first number:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
parseFloat(343).toFixed(2) + "<br>" +
parseFloat("10").toFixed(2) + "<br>" +
parseFloat("7.33").toFixed(2) + "<br>" +
parseFloat("2 05 96").toFixed(2) + "<br>" +
parseFloat("its 100").toFixed(2);
</script></body>
</html>```
1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
5. Here, as you see that we create one heading, paragraph and then again paragraph. There is no meaning to use these all here. You can directly use script tag.
6. Now, script tag is a paired tag with ending </script>. Our all JavaScript code is written under this pair of tags.
7. One thing to note that there must be one paragraph or say anything there in body to show text from script tag.
8. Inside script tag, first we target the paragraph which we created to show text/outputs from JavaScript. And parsefloat is used to convert string to float here whereas tofixed is used to give decimal at last. Here we give value 2.
9. Hence, two decimals are showed there at last. Whereas prasefloat is used to convert string to float.
10. You can customize decimals by changing value in toFixed. And parse float will give error if it is unable to found number at first in string.
11. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

## Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to convert string to float with two decimals.

I hope this tutorial on JavaScript string to float 2 decimal helps you and the steps and method mentioned above are easy to follow and implement.