Udostępnij za pośrednictwem


Numeric Data

The choice between the two types of numeric data in JScript, integral data and floating-point data, depends on the particular circumstances in which they are used. There are also different ways of representing integral data and floating-point data literally.

Positive whole numbers, negative whole numbers, and the number zero are integers. They can be represented in base 10 (decimal), base 8 (octal), and base 16 (hexadecimal). Most numbers in JScript are written in decimal. To denote octal integers, prefix them with a leading 0 (zero). They can contain digits 0 through 7 only. A number with a leading 0, containing the digits 8 and/or 9 is interpreted as a decimal number. Use of octal numbers is not generally recommended.

To denote hexadecimal (hex) integers, prefix them with a leading "0x" (zero and x|X). They can contain digits 0 through 9, and letters A through F (either uppercase or lowercase) only. The letters A through F represent, as single digits, 10 through 15 in base 10. That is, 0xF is equivalent to 15, and 0x10 is equivalent to 16.

Both octal and hexadecimal numbers, which can be negative, cannot have a decimal portion and cannot be written in scientific (exponential) notation.

Floating-point values are whole numbers with a decimal portion. Like integers, they can be represented literally with digits followed by a decimal point and more digits. Additionally, they can be expressed in scientific notation. That is, an uppercase or lowercase letter e is used to represent "times ten to the power of". A number that begins with a single 0 and contains a decimal point is interpreted as a decimal floating-point literal and not an octal literal.

Additionally, floating-point numbers in JScript can represent special numerical values that integral data types cannot. These are:

  • NaN (not a number). This is used when a mathematical operation is performed on inappropriate data, such as strings or the undefined value.

  • Infinity. This is used when a positive number is too large to represent in JScript.

  • -Infinity (negative Infinity) This is used when the magnitude of a negative number is too large to represent in JScript.

  • Positive and Negative 0. JScript differentiates between positive and negative zero in some situations.

Here are some examples of JScript numbers. Notice that a number that begins with "0x" and contains a decimal point will generate an error.

Number

Description

Decimal Equivalent

.0001, 0.0001, 1e-4, 1.0e-4

Four equivalent floating-point numbers.

0.0001

3.45e2

A floating-point number.

345

42

An integer.

42

0378

An integer. Although this looks like an octal number (it begins with a zero), 8 is not a valid octal digit, so the number is treated as a decimal. This produces a level 1 warning.

378

0377

An octal integer. Notice that although it appears to be only one less than the number above, its actual value is quite different.

255

0.0001, 00.0001

A floating point number. Even though this begins with a zero, it is not an octal number because it has a decimal point.

0.0001

0Xff

A hexadecimal integer.

255

0x37CF

A hexadecimal integer.

14287

0x3e7

A hexadecimal integer. Notice that the letter e is not treated as exponentiation.

999

0x3.45e2

This is an error. Hexadecimal numbers cannot have decimal parts.

N/A (compiler error)

Variables of any integral data type can represent only a finite range of numbers. If you attempt to assign a numeric literal that is too large or too small to an integral data type, a type-mismatch error will be generated at compile time. For more information, see Data Type Summary.

Data Types of Literals

In most situations, the data type that JScript interprets numeric literals as is inconsequential. However, when the numbers are very large or very precise, these details are important.

Integer literals in JScript can represent data of type int, long, ulong, decimal, or double, depending on the size of the literal and its use. Literals in the range of the int type (-2147483648 to 2147483647) are interpreted as type int. Literals outside of that range but within the range of the long type (-9223372036854775808 to 9223372036854775807) are interpreted as long. Literals outside of that range but within the range of the ulong type (9223372036854775807 to 18446744073709551615) are interpreted as ulong. All others integer literals are interpreted as type double, which entails a loss of precision. An exception to the last rule is that the literal will be interpreted as a decimal if the literal is immediately stored in a variable or constant typed as decimal, or if it is passed to a function that is typed to receive a decimal.

A JScript floating-point literal is interpreted as the data type double, unless the literal is immediately used as decimal (like integer literals), in which case the literal is interpreted as a decimal. The decimal data type cannot represent NaN, positive Infinity, or negative Infinity.

See Also

Reference

NaN Property (Global)

Infinity Property

Concepts

JScript Expressions

Other Resources

Data in JScript

Data Types (Visual Studio - JScript)