[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Guide/Numbers_and_strings#math_object [Back]  [Original]

- JavaScript | MDN

MDN Web Docs

View in English Always switch to English

JavaScript

In this article

(Numbers)

JavaScript , Number double-precision 64-bit binary format IEEE 754 (i.e. number -(2^53 -1) 2^53 -1)

: +Infinity -Infinity and NaN (not-a-number)

BigInt Javascript BigIntBigInt Number operation Math BigInt

JavaScript data types and structures

(decimal) (binary) (octal) (hexadecimal)

js
1234567890;
42;

// 

0888; // 888  
0777; //  non-strict  ( 511)

(0) 8( 0888 888 068 68)( 0777 511 063 51)

0 B (0b 0B) 0b 0 1 SyntaxError(): "Missing binary digits after 0b"

js
var FLT_SIGNBIT = 0b10000000000000000000000000000000; // 2147483648
var FLT_EXPONENT = 0b01111111100000000000000000000000; // 2139095040
var FLT_MANTISSA = 0b00000000011111111111111111111111; // 8388607

0 0 0 7

js
var n = 0755; // 493
var m = 0644; // 420

Strict mode in ECMAScript 5 forbids octal syntax. Octal syntax isn't part of ECMAScript 5, but it's supported in all browsers by prefixing the octal number with a zero: 0644 === 420 and"\045" === "%". In ECMAScript 2015, octal numbers are supported if they are prefixed with 0o, e.g.:

js
var a = 0o10; // ES2015: 8

0 X(0x 0X) 0b (0123456789ABCDEF) SyntaxError():"Identifier starts immediately after numeric literal"

js
0xfffffffffffffffff; // 295147905179352830000
0x123456789abcdef; // 81985529216486900
0xa; // 10

js
1e3; // 1000
2e6; // 2000000
0.1e2; // 10

Number

The built-in Number object has properties for numerical constants, such as maximum value, not-a-number, and infinity. You cannot change the values of these properties and you use them as follows:

js
var biggestNum = Number.MAX_VALUE;
var smallestNum = Number.MIN_VALUE;
var infiniteNum = Number.POSITIVE_INFINITY;
var negInfiniteNum = Number.NEGATIVE_INFINITY;
var notANum = Number.NaN;

You always refer to a property of the predefined Number object as shown above, and not as a property of a Number object you create yourself.

Number

Number

Number.MAX_VALUE
Number.MIN_VALUE
Number.NaN Not-A-Number
Number.NEGATIVE_INFINITY Special negative infinite value; returned on overflow
Number.POSITIVE_INFINITY Special positive infinite value; returned on overflow
Number.EPSILON Difference between one and the smallest value greater than one that can be represented as a Number.
Number.MIN_SAFE_INTEGER JavaScript
Number.MAX_SAFE_INTEGER JavaScript
Number.parseFloat() parseFloat()
Number.parseInt() parseInt()
Number.isFinite()
Number.isInteger()
Number.isNaN() Determines whether the passed value is NaN. More robust version of the original global isNaN().
Number.isSafeInteger() Determines whether the provided value is a number that is a safe integer.

The Number prototype provides methods for retrieving information from Number objects in various formats. The following table summarizes the methods of Number.prototype.

toExponential() Returns a string representing the number in exponential notation.
toFixed() Returns a string representing the number in fixed-point notation.
toPrecision() Returns a string representing the number to a specified precision in fixed-point notation.

Math

The built-in Math object has properties and methods for mathematical constants and functions. For example, the Math object's PI property has the value of pi (3.141...), which you would use in an application as

js
Math.PI;

Similarly, standard mathematical functions are methods of Math. These include trigonometric, logarithmic, exponential, and other functions. For example, if you want to use the trigonometric function sine, you would write

js
Math.sin(1.56);

Note that all trigonometric methods of Math take arguments in radians.

The following table summarizes the Math object's methods.

abs()
sin(), cos(), tan() ;
asin(), acos(), atan(), atan2() ;
sinh(), cosh(), tanh() ; hyperbolic angle
asinh(), acosh(), atanh() ; hyperbolic angle
pow(), exp(), expm1(), log10(), log1p(), log2()
floor(), ceil() //
min(), max() Returns lesser or greater (respectively) of comma separated list of numbers arguments
random() 0 1
round(), fround(), trunc(), Rounding and truncation functions.
sqrt(), cbrt(), hypot() Square root, cube root, Square root of the sum of square arguments.
sign() The sign of a number, indicating whether the number is positive, negative or zero.
clz32(), imul() Number of leading zero bits in the 32-bit binary representation. The result of the C-like 32-bit multiplication of the two arguments.

Unlike many other objects, you never create a Math object of your own. You always use the built-in Math object.

Date

JavaScript (date)(data type) Date Date (method)

JavaScript Java date 1970 1 1 0 0 0 (millisecond)

Date -100,000,000 days to 100,000,000 days 1970 1 1 0 0 0 UTC

Date:

js
var dateObjectName = new Date([parameters]);

dateObjectName Date

Calling Date without the new keyword returns a string representing the current date and time.

The parameters in the preceding syntax can be any of the following:

  • Nothing: creates today's date and time. For example, today = new Date();.
  • A string representing a date in the following form: "Month day, year hours:minutes:seconds." For example, var Xmas95 = new Date("December 25, 1995 13:30:00"). If you omit hours, minutes, or seconds, the value will be set to zero.
  • A set of integer values for year, month, and day. For example, var Xmas95 = new Date(1995, 11, 25).
  • A set of integer values for year, month, day, hour, minute, and seconds. For example, var Xmas95 = new Date(1995, 11, 25, 9, 30, 0);.

Date

The Date object methods for handling dates and times fall into these broad categories:

  • "set" methods, for setting date and time values in Date objects.
  • "get" methods, for getting date and time values from Date objects.
  • "to" methods, for returning string values from Date objects.
  • parse and UTC methods, for parsing Date strings.

With the "get" and "set" methods you can get and set seconds, minutes, hours, day of the month, day of the week, months, and years separately. There is a getDay method that returns the day of the week, but no corresponding setDay method, because the day of the week is set automatically. These methods use integers to represent these values as follows:

  • Seconds and minutes: 0 59
  • Hours: 0 23
  • Day: 0 () 6 ()
  • Date: 1 31 ()
  • Months: 0 () 11 ()
  • Year: years since 1900

js
var Xmas95 = new Date("December 25, 1995");

Xmas95.getMonth() 11 Xmas95.getFullYear() 1995

getTime setTime The getTime method returns the number of milliseconds since January 1, 1970, 00:00:00 for a Date object.

For example, the following code displays the number of days left in the current year:

js
var today = new Date();
var endYear = new Date(1995, 11, 31, 23, 59, 59, 999); // Set day and month
endYear.setFullYear(today.getFullYear()); // Set year to this year
var msPerDay = 24 * 60 * 60 * 1000; // Number of milliseconds per day
var daysLeft = (endYear.getTime() - today.getTime()) / msPerDay;
var daysLeft = Math.round(daysLeft); //returns days left in the year

This example creates a Date object named today that contains today's date. It then creates a Date object named endYear and sets the year to the current year. Then, using the number of milliseconds per day, it computes the number of days between today and endYear, using getTime and rounding to a whole number of days.

The parse method is useful for assigning values from date strings to existing Date objects. For example, the following code uses parse and setTime to assign a date value to the IPOdate object:

js
var IPOdate = new Date();
IPOdate.setTime(Date.parse("Aug 9, 1995"));

JSClock()

js
function JSClock() {
  var time = new Date();
  var hour = time.getHours();
  var minute = time.getMinutes();
  var second = time.getSeconds();
  var temp = "" + (hour > 12 ? hour - 12 : hour);
  if (hour == 0) temp = "12";
  temp += (minute < 10 ? ":0" : ":") + minute;
  temp += (second < 10 ? ":0" : ":") + second;
  temp += hour >= 12 ? " P.M." : " A.M.";
  return temp;
}

JSClock time Date ; getHours getMinutes getSeconds hour minute second

temp; hour 12 (hour - 12) hour hour 0 12

minute temp minute 10, ; temp

hour 12 temp "P.M." "A.M."


Web Proxy Viewer  |  New URL  |  Original Page