Monday, August 8, 2016

String to Integer conversion in JavaScript

http://stackoverflow.com/questions/1133770/how-do-i-convert-a-string-into-an-integer-in-javascript
http://jsbin.com/vavotujoge/edit?html,js,output
========================================================
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div id="rizvi">
  <input type="textbox" id="Tue" />
  <input type="textbox" id="Wed" />
  <input type="textbox" id="Thu" />
  <input type="textbox" id="Sat" />
  </div>
</body>
</html>

=======================================================
var textbox = document.getElementById('Tue');
textbox.value = ("00000000" + "12345678").slice(-8);

var textbox1 = document.getElementById('Wed');
textbox1.value = parseInt("010", 10);

var textbox2 = document.getElementById('Thu');
textbox2.value = parseInt("00010101", 10);

var textbox3 = document.getElementById('Sat');
var str = "000000000000000034566789900233";       // string
var number = str*1;   // number
textbox3.value = number;
=======================================================
Output:
12345678
10
10101
34566789900233

No comments:

Post a Comment