Home > Web Front-end > JS Tutorial > body text

How to complete numbers in js

(*-*)浩
Release: 2020-09-18 13:35:58
Original
3994 people have browsed it

JS method of completing numbers: 1. Through iteration; 2. Through the "num/Math.pow(10, length);" method; 3. Through "(Array(length).join ('0') num).slice(-length);" is implemented.

How to complete numbers in js

For example, we want the length of the number to be output to be fixed, assuming it is 10. If the number is 123, then 0000000123 will be output. If there are not enough digits, add 0 before it. . Of course, you can also change the number you want to add based on the code in this chapter.

Here are three different ways to implement the operation of adding 0 to a number using JS code:

Method 1: Iterative implementation

function PrefixInteger(num, length) {
  for(var len = (num + "").length; len < length; len = num.length) {
   num = "0" + num;
   }
  return num;
}
Copy after login

Method 2: Convert to decimal

function PrefixInteger(num, length) {
 var decimal = num / Math.pow(10, length);
  //toFixed指定保留几位小数
  decimal = decimal.toFixed(length) + "";
  return decimal.substr(decimal.indexOf(".")+1);
}
Copy after login

Method 3: More efficient

function PrefixInteger(num, length) {
 return (Array(length).join(&#39;0&#39;) + num).slice(-length);  
}
Copy after login

Test:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript 数字前补“0”</title>
<body>
<script>
 //迭代方式实现
 function padding1(num, length) {
  for(var len = (num + "").length; len < length; len = num.length) {
   num = "0" + num;
  }
  return num;
 }
//转为小数
 function padding2(num, length) {
  var decimal = num / Math.pow(10, length);
  //toFixed指定保留几位小数
  decimal = decimal.toFixed(length) + "";
  return decimal.substr(decimal.indexOf(".")+1);
 }
 //填充截取法
 function padding3(num, length) {
  //这里用slice和substr均可
  return (Array(length).join("0") + num).slice(-length);
 }

 function test(num, length) {
  document.write(padding1(num, length));
  document.write("<br>");
  document.write(padding2(num, length));
  document.write("<br>");
  document.write(padding3(num, length));
 }
 test(123, 10);
</script>
</body>
</html>
Copy after login

Result:

0000000123  
0000000123  
0000000123
Copy after login

The above is the detailed content of How to complete numbers in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template