• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / JavaScript / JavaScript toFixed – How to Convert a Number to a String

JavaScript toFixed – How to Convert a Number to a String

September 29, 2022 Leave a Comment

The JavaScript toFixed method will take a number and convert it to a string. You can also pass the toFixed() method a number and it will round the number to that many decimal places. If no number is given, it will just round the number to an Integer.

var num = 10.7354;
var numToString = num.toFixed();

console.log(numToString);

#Output
11

Let’s take a look at some more examples of the JavaScript toFixed() method.


The best way to learn how to use the JavaScript toFixed() method is to see it in action. Here are some more examples of it below.

var num = 2.485487;
var numToString1 = num.toFixed();
var numToString2 = num.toFixed(1);
var numToString3 = num.toFixed(2);
var numToString4 = num.toFixed(3);
var numToString5 = num.toFixed(8);

console.log(numToString1);
console.log(numToString2);
console.log(numToString3);
console.log(numToString4);
console.log(numToString5);

#Output
2
2.5
2.49
2.485
2.48548700

As you can see in our last example num.toFixed(8);, if you pass a number that is larger than the number of decimal places in our number, zeros will be added to the end of the number.

Now let’s take an interactive look at the toFixed() method.

Using the JavaScript toFixed Method with User Input

Below we will provide code to let the user input two numbers. The first will be a number the user wants to be converted to a string. The second will be the number of decimal places to round to.

Here is our simple HTML setup:

<p>Type a number you want converted to a string:</p>
<input id="userVal1" type="number">
<p>Type the number of decimal places you want the number rounded to:</p>
<input id="userVal2" type="number">
<input id="submitNum" type="submit" value="Submit" onclick="convertNumtoString()">
<div id="results"></div>

Below is the JavaScript code which will take the user inputs using an onclick event along with the value property and use the toFixed() method on that user input and update the results below using the textContent property.

Here is our JavaScript code to do this:

function convertNumtoString() {
  var roundedNumber;
  //Get the number the user entered
  var userNum = Number(document.getElementById("userVal1").value);
    
  //Get the number of decimal places the user entered
  var userNum2 = Number(document.getElementById("userVal2").value);
    
  //Convert Number to string to the decimal places entered
  var numToString = userNum.toFixed(userNum2);

  //Show the number to the user
  document.getElementById("results").textContent = numToString;   
};

The final code and output for this example are below:

Code Output:

Type a number you want converted to a string:

Type the number of decimal places you want the number rounded to:


Full Code:

<p>Type a number you want converted to a string:</p>
<input id="userVal1" type="number">
<p>Type the number of decimal places you want the number rounded to:</p>
<input id="userVal2" type="number">
<input id="submitNum" type="submit" value="Submit" onclick="convertNumtoString()">
<div id="results"></div>

<script>

function convertNumtoString() {
  var roundedNumber;
  //Get the number the user entered
  var userNum = Number(document.getElementById("userVal1").value);
    
  //Get the number of decimal places the user entered
  var userNum2 = Number(document.getElementById("userVal2").value);
    
  //Convert Number to string to the decimal places entered
  var numToString = userNum.toFixed(userNum2);

  //Show the number to the user
  document.getElementById("results").textContent = numToString;   
};

</script>

Hopefully this article has been useful in helping you understand how to use the JavaScript toFixed method.

Other Articles You'll Also Like:

  • 1.  getDate JavaScript – Get the Current Day of the Month in JavaScript
  • 2.  Using JavaScript to Generate a Random Float Between 0 and 1
  • 3.  Using JavaScript to Get a Random Number Between 1 and 5
  • 4.  Using JavaScript to Change Text of Element
  • 5.  setTimeout javascript – How the setTimeout() Method in JavaScript Works
  • 6.  Using JavaScript to Get Substring Between Two Characters
  • 7.  JavaScript asinh – Find Hyperbolic Arcsine of Number Using Math.asinh()
  • 8.  Using JavaScript to Reverse a Number
  • 9.  Sort List of Divs with Data Attribute with Radio Buttons Using Javascript
  • 10.  JavaScript join – Create String from Elements in an Array

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy