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

Summary of operations on numbers in js (code)

不言
Release: 2018-08-23 15:27:21
Original
2387 people have browsed it

The content of this article is about the summary (code) of operations on numbers in js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Get the value of the bit-th bit in the binary form of the number num. Note:
1. Bit starts from 1
2. Returns 0 or 1
3. Example: the binary number of 2 is 10, the first bit is 0, and the second bit is 1

function valueAtBit(num, bit) {
  var s = num.toString(2);     
  return s[s.length - bit];
 }
 function valueAtBit(num, bit) {
    //toString转化为二进制,split将二进制转化为数组,reverse()将数组颠倒顺序
    var arr = num.toString(2).split("").reverse();    
    return arr[bit-1];
}
function valueAtBit(num, bit) {
    return (num >> (bit -1)) & 1;
}
Copy after login

2. Given a binary string, convert it into the corresponding decimal number

function base10(str) {
    return parseInt(str,2);
}
Copy after login

3. Convert the given number into a binary string. If the string length is less than 8 digits, add 0 to the front to reach 8 digits.

function convertToBinary(num) {
     //转换为2进制格式
     var s = num.toString(2);     
     //获得2进制数长度
     var l = s.length;     
     if(l<8){         
     //声明一个字符串用于补满0
         var s1 = "0000000";         
         var s2 = s1.slice(0,8-l);
         s = s2+s; 
     }     return s;
 }
 function convertToBinary(num) {
    var str = num.toString(2);    
    while(str.length < 8) {
        str = "0" + str;
    }    
    return str;
}
function convertToBinary(num) {
    var s =  num.toString(2);    
    return &#39;00000000&#39;.slice(s.length) + s;
}
function convertToBinary(num) {
  return (&#39;00000000&#39; + num.toString(2)).slice(-8); //从倒数第八个开始取}
Copy after login

4. Find the value of multiplying a and b. A and b may be decimals. You need to pay attention to the accuracy of the result.

//根据两个书中精度较高的一个来确定,先将其转换成字符串,然后根据小数点的位置确定小数位数,
//字符串长度减去‘.&#39;的位置后还要再减去1得到正确的小数位数,两个取其大,然后用toFixed()函数确定 
//结果的小数位数
function multiply(a, b) {
   //求两个数中精度大的一个
    var stra=a.toString();    
    var strb=b.toString();    
    var len=Math.max(stra.length-stra.indexOf(&#39;.&#39;)-1,strb.length-strb.indexOf(&#39;.&#39;)-1);    //
    return parseFloat(a*b).toFixed(len);
}
Copy after login

Related recommendations:

Summary of methods for operating arrays in js (code)

A brief analysis of the content of js event binding & event listening & event delegation

The above is the detailed content of Summary of operations on numbers in js (code). 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