</> Code Corner

JavaScript · JS Tutorial · Lesson 18

JS Number Methods

toFixed, toString, Number(), parseInt and parseFloat.

Number methods help you work with numbers and convert values to numbers.

Example

<p id="demo"></p>
<script>
let x = 9.656;
let out = "";
out += x.toFixed(2) + "<br>";
out += x.toString() + " (string)<br>";
out += (255).toString(16) + " (hex)<br>";
out += Number("10.33") + "<br>";
out += parseInt("10.33 years") + "<br>";
out += parseFloat("10.33 years") + "<br>";
out += Number("John");
document.getElementById("demo").innerHTML = out;
</script>
Method Does
toFixed(n) Rounds to n decimals, returns a string
toString(base) Converts to a string (optionally in another base)
Number(value) Converts a value to a number
parseInt() / parseFloat() Parses a string and returns an integer / float
Number.isInteger() Checks for an integer

Keep reading “JS Number Methods”

Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.

Chat with us 👋