8

I'd like to know if it is possible to use 2 digits in number in JavaScript if the number is less than 10?

for example

10,9,8,7,6 and so on to

10,09,08,07,06 as "number data type"

yes, it's possible to display the numbers 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 number data type so I can use

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

is it possible?

3
  • 3
    What is the reason for wanting to use 01 as apposed to 1 on a JavaScript function? I can understand for display purposes but not for logic. Commented Jan 29, 2016 at 10:10
  • 2
    Who wouldn't want to use the number one as 01? It's very stylish :P Commented Jan 29, 2016 at 10:13
  • 1
    You could create a pseudotype that will return the number value or a string value for display that appends a 0 if it is less than 10. However this seems overkill and a bit inefficient. Simply, before you show a number, pass it through a function that converts it to a string, appending 0 if the number less than 10. Commented Jan 29, 2016 at 10:15

4 Answers 4

11

ParseInt Syntax

Reference

parseInt(string, radix);

Parameters

string The value to parse. If string is not a string, then it is converted to a string (using the ToString abstract operation). Leading whitespace in the string is ignored.

radix An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified, usually defaulting the value to 10.

Do not do

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

correct Answer

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

Check this in console

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

Number

The Number JavaScript object is a wrapper object allowing you to work with numerical values. A Number object is created using the Number() constructor. A primitive type object number is created using the Number() function.

Example

Number('123')     // 123
Number('12.3')    // 12.3
Number('12.00')   // 12
Number('123e-1')  // 12.3
Number('')        // 0
Number(null)      // 0
Number('0x11')    // 17
Number('0b11')    // 3
Number('0o11')    // 9
Number('foo')     // NaN
Number('100a')    // NaN
Number('-Infinity') //-Infinity

enter image description here

Image Reference

Sign up to request clarification or add additional context in comments.

Comments

2

It would work, but it wouldn't do what you want it to do. Numbers with leading zeros are literals for octal numbers (positional number system with the base of 8). So for example:

071 == 71
=> false

071 == 57
=> true

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

1 Comment

this doesn't work with his original code, as number is string there, you compare "071" == 071
1

It works with parseInt. By default it's base 10. So even 09 is ok, but as @deceze pointed out it's better to explicitly use the radix 10:

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

7 Comments

Be very aware that you're using octal notation here and what that may mean for your program logic!!
It's not "wrong", it just happens to work without side effects in this case. You're still using octal, and extending this to all possible situations may result in side effects. What if you want to "zero pad" up to 3 digits?
09 does not exist and Javascript happens to fall back to interpreting it as 9. Again, I'm saying that you must be aware of this, because the same won't be true for, e.g., 071.
parseInt("09") in older versions of IE will not parse correctly due to being interpreted as octal. This has since changed in modern browsers, but this is hardly a solution that works in every case. You need to use parseInt("09", 10) instead.
Now you're just deliberately screwing around... :P
|
0

Binary or decimal 01 is still one.

<!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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.