JavaScript Tutorial/Number Data Type/Integer
Содержание
- 1 Calculation on integers
- 2 Compare two numbers
- 3 Define and use integers
- 4 Generate random number
- 5 Integers
- 6 Integers can also be represented as either octal (base 8) or hexadecimal (base 16) literals.
- 7 To create a hexadecimal literal, the first digit must be a zero (0) followed by the letter x, followed by any number of hexadecimal digits (0-9 and A-F).
- 8 Using Binary Flags
Calculation on integers
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
var a = 6, b = 4;
alert(a - b);
// -->
</script>
</head>
<body>
</body>
</html>
Compare two numbers
<HTML>
<HEAD>
</HEAD>
<BODY>
<SCRIPT language="JavaScript">
<!--
var num1=0;
var num2=0;
if(num1==num2) {
window.alert("True");
}
else {
window.alert("False");
}
//-->
</SCRIPT>
</BODY>
</HTML>
Define and use integers
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
var a = 6, b = 4;
alert(a + b);
// -->
</script>
</head>
<body>
</body>
</html>
Generate random number
<html>
<head>
<title>numberMaker</title>
<script>
var number;
number = Math.random();
alert ("Here"s my number: " + number);
</script>
</head>
<body>
<hr>
<h3>Hit the Reload key to see a new number!</h3>
</body>
</html>
Integers
Integers are numbers that contain no fractional parts.
Integers are numbers that can be positive or negative.
Integers are numbers that can be formatted as a decimal, octal, or hexadecimal in JavaScript.
Decimal integers is made up of numbers from 0 to 9 and cannot begin with leading zeros.
Octal integers, also referred to as base-8, must begin with a leading zero. Each digit following the leading zero can be 0 to 7.
Hexadecimal integers, also referred to as base-16, must begin with 0x or 0X. Each digit following the leading zero can be 0 through 15, but 10 through 15 are represented by the letters a (or A) through f (or F).
5. 3. Integer 5. 3. 1. Integers 5. 3. 2. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/Integerscanalsoberepresentedaseitheroctalbase8orhexadecimalbase16literals.htm">Integers can also be represented as either octal (base 8) or hexadecimal (base 16) literals.</a> 5. 3. 3. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/Tocreateahexadecimalliteralthefirstdigitmustbeazero0followedbytheletterxfollowedbyanynumberofhexadecimaldigits09andAF.htm">To create a hexadecimal literal, the first digit must be a zero (0) followed by the letter x, followed by any number of hexadecimal digits (0-9 and A-F).</a> 5. 3. 4. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/Defineanduseintegers.htm">Define and use integers</a> 5. 3. 5. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/Calculationonintegers.htm">Calculation on integers</a> 5. 3. 6. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/Comparetwonumbers.htm">Compare two numbers</a> 5. 3. 7. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/Generaterandomnumber.htm">Generate random number</a> 5. 3. 8. <A href="/Tutorial/JavaScript/0100__Number-Data-Type/UsingBinaryFlags.htm">Using Binary Flags</a>
Integers can also be represented as either octal (base 8) or hexadecimal (base 16) literals.
For an octal literal, the first digit must be a zero (0).
The following digits can be any octal digit (0 through 7).
var iNum = 070;
To create a hexadecimal literal, the first digit must be a zero (0) followed by the letter x, followed by any number of hexadecimal digits (0-9 and A-F).
The digits may be in uppercase or lowercase.
var iNum = 0x1f;
var iNum2 = 0xAB;
Using Binary Flags
<html>
<head>
<title>Using Binary Flags</title>
<script type="text/javascript">
var FIELD_A = 0x1; // 00001
var FIELD_B = 0x2; // 00010
var FIELD_C = 0x4; // 00100
var FIELD_D = 0x8; // 01000
var FIELD_E = 0x10; // 10000
var fieldsSet = FIELD_A | FIELD_C | FIELD_E;
if ((fieldsSet & FIELD_A) && (fieldsSet & FIELD_C)) {
document.write("Fields A and C are set");
}
</script>
</head>
</body>
</html>