Format numbers to a chosen number of decimal places in JavaScript

Description

The following code shows how to format numbers to a chosen number of decimal places.

Example


<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function formatNumber(theNum, numDecPlaces)
{<!--from   www.  java  2s .c om-->
var num = new String();
num = "" + theNum;
var pos = 0;
count = 0;
while (num.substring(pos-1,pos)!== ".") {
pos += 1 ;
count += 1;
}
while (pos < (count+numDecPlaces)){
pos +=1;
}
return num.substring(0,pos);
}

var entry;
var first;
var second;
var numPlaces;
first = Number(1);
second = Number(3);
numPlaces = Number(2);
var n = first / second;

document.write(n+"  vs  "+formatNumber(n,numPlaces));

</script>
</head>
<body>
</body>
</html>

Click to view the demo

The code above generates the following result.

Format numbers to a chosen number of decimal places in JavaScript
Home »
  Javascript Tutorial »
    Data Type »
      Number
Javascript Tutorial Number
Add function to Number.prototype in JavaScr...
Add the float point numbers together in Jav...
Calculate Exponential for a number with toE...
Check if a string is a Not-A-Number with is...
Check if a string is representing a number ...
Check that Octal Integer literal can only h...
Compare NaN to a string in JavaScript
Compare NaN with other value in JavaScript
Compare a number to Number.NEGATIVE_INFINIT...
Compare a number to Number.NaN in JavaScrip...
Compare number to Number.POSITIVE_INFINITY ...
Compare number value to Number.MAX_VALUE in...
Compare number value to Number.MIN_VALUE in...
Compare two numbers for equal in JavaScript
Compare with positive Infinity in JavaScrip...
Crate a Not a Number in JavaScript
Create Floating-Point Numbers in scientific...
Create Hexadecimal integer in JavaScript
Create Number Object from literal in JavaSc...
Create Octal Integer literal by setting the...
Create a float Literal with no decimal part...
Create a float point type value in JavaScri...
Create a new number object from number lite...
Create a number type varible with number li...
Create a number with Scientific notation li...
Create a scientific notation saying take 1....
Create float type number with literal in Ja...
Create integer from a hexadecimal literal i...
Define and use integers in JavaScript
Divide two integers in JavaScript
Format numbers to a chosen number of decima...
Generate a random number in JavaScript
Get number value for the negative infinity ...
Get number value for the positive infinity ...
Get the max value for a number in JavaScrip...
Get the min value for a number in JavaScrip...
Get the primitive value of a Number object ...
Round decimal value up in JavaScript
Round number to a number with a specified n...
String with only number and minus sign, for...
Tell if the value is "not a number" with is...
Use Binary Flags with binary operators in J...
Use hexadecimal numbers in calculation in J...
Use if statement to check if a variable is ...
Use isFinite function to check if a number ...
Use isNaN to test boolean literal in JavaSc...
Use isNan to check if a value is a number i...
Use octal numbers in calculation in JavaScr...