How to convert string "01" to number in JavaScript?

I would like to know if 2 digits can be used in JavaScript if the number is less than 10?

eg

10,9,8,7,6 and so on 10,09,08,07,06 as "data type data"

yes, it is possible to display digits in 2 digits when the numbers are less than 10 as a "string data type".

if(number < 10) {
    console.log(number) //number
    number = "0" + number;
    console.log(number) //string
}

but I want to use it as a data data type, so I can use

if(number == 01) {
    //some code here
}

Is it possible?

+4
source share
4 answers

ParseInt Syntax

Reference

parseInt (string, radix);

Parameters

string Value for parsing. If the string is not a string, then it is converted to a string (using the abstract ToString operation). Leading spaces in a line are ignored.

radix 2 36, ( ) . 10 . . , , 10.

var number = "09";
if(number == 09) { // here it will not compare the type check the == and ===
    alert("OK: " + number)
} else {
    alert("PROBLEM: " + number);
}

var number = "09";
var decimal = parseInt(number,10);
if(decimal === 09) {
    alert("OK: " + decimal)
} else {
    alert("PROBLEM: " + decimal);
}

var first = 10 ;
var secn = "10";

first == secn // true because both are equal. 
first === secn // both are not equal by type(string and number)



var result = parseInt("010", 10) == 10; // Returns true

var result = parseInt("010") == 10; // Returns false
+6

, , . - ( 8). , :

071 == 71
=> false

071 == 57
=> true

: http://www.w3schools.com/js/js_numbers.asp

+2

parseInt. 10. 09 , @deceze , radix 10:

var number = "09";
var decimal = parseInt(number, 10);
if(decimal == 09) {
    alert("OK: " + decimal)
} else {
    alert("PROBLEM: " + decimal);
}
Hide result
+1

01 .

<!DOCTYPE html>
<html>

<head>
  <style>
    html,
    body {
      box-sizing: border-box;
      font: small-caps 500 16px/1.4'Consolas';
      width: 100vw;
      height: 100vh;
      background: #222;
      color: lime;
      line-height: 1;
    }
    *,
    *:before,
    *:after {
      box-sizing: inherit;
      margin: 0;
      padding: 0;
    }
    fieldset {
      margin: 1.5em auto;
      padding: 5px;
      min-width: 100px;
      max-width: 50%;
      border: 3px ridge green;
      border-radius: 8px;
    }
    legend {
      font-size: 1.5rem;
    }
    input {
      width: 3ch;
      padding: 2px 1px;
      border: 2px inset green;
      border-radius: 4px;
      background: #000;
      font-size: 1.1rem;
      color: lime;
      font: inherit;
    }
    #msg {
      color: #00FA9A;
    }
  </style>
</head>

<body>
  <fieldset>
    <legend>zer00ne</legend>
    <input id="inp1" oninput="bin(this.value);" />
    <input id="out1" readonly/>
    <span id="msg">Enter 01</span>
  </fieldset>
  <script>
    function bin(x) {
      var msg = document.getElementById('msg');
      var y = document.getElementById('out1');
      var z = parseInt(x, 2);
      y.value = z;
      msg.innerHTML += ", binary or decimal I'm number One";
    }
  </script>
</body>

</html>
Hide result
0
source

Source: https://habr.com/ru/post/1626499/


All Articles