javascript number


But none of us can deny that math is a fundamental part of life that we can't get very far without
但我们谁也不能否认,数学是生活的一个基本组成部分,没有它我们就走不远 


  • Integers are floating-point numbers without a fraction. They can either be positive or negative, e.g. 10, 400, or -5.
  • Floating point numbers (floats) have decimal points and decimal places, for example 12.5, and 56.7786543.
  • Doubles are a specific type of floating point number that have greater precision than standard floating point numbers (meaning that they are accurate to a greater number of decimal places).


JavaScript only has one data type for numbers, both integers and decimals — you guessed it, Number. This means that whatever type of numbers you are dealing with in JavaScript, you handle them in exactly the same way.

Note: Actually, JavaScript has a second number type, BigInt, used for very, very large integers. But for the purposes of this course, we'll just worry about Number values.


Number的methods The Number object,
function和method的区别在前面介绍过。一组functions组合成对象的methods


let lotsOfDecimal = 1.766584958675746364;
lotsOfDecimal;
let twoDecimalPlaces = lotsOfDecimal.toFixed(2);
twoDecimalPlaces;

转换为number

passing the string value into the Number() constructor  to return a number version of the same value.

let myNumber = '74';
myNumber + 3

you end up with the result 743, not 77, because myNumber is actually definded as a string.  You can test this by typing in the following:

typeof myNumber;

To fix the calculation, you can do this:

Number(myNumber) + 3;
Arithmetic operators are the basic operators that we use to do sums in JavaScript:

OperatorNamePurposeExample+ | Addition | Adds two numbers together. | 6 + 9
- | Subtraction | Subtracts the right number from the left. | 20 - 15
* | Multiplication | Multiplies two numbers together. | 3 * 7
/ | Division | Divides the left number by the right. | 10 / 5
% | Remainder (sometimes called modulo) | Returns the remainder left over after you've divided the left number into a number of integer portions equal to the right number. | 8 % 3 (returns 2, as three goes into 8 twice, leaving 2 left over).
** | Exponent| Raises a base number to the exponent power, that is, the base number multiplied by itself, exponent times. It was first Introduced in EcmaScript 2016. | 5 ** 2 (returns 25, which is the same as 5 * 5).

exponent  美 [ɪkˈspoʊnənt] 
指数  (观点、理论的)拥护者,鼓吹者,倡导者;(某种活动的)能手,大师;指数;幂
ex 出 + pon 放,放置 + ent 表人 → 把〔内容〕讲出来 → 讲解者


+=
-=
*=
/=




The strict versions tend to result in fewer errors, so we recommend you use them.
严格版本tend to 导致较少的结果,因此我们建议你用他们, ===和!==


Test
// Statement 1: The elephant weights less than the mouse
let eleWeight = 1000;
let mouseWeight = 2;

// Statement 2: The Ostrich is taller than the duck
let ostrichHeight = 2;
let duckHeight = 0.3;

// Statement 3: The two passwords match
let pwd1 = 'stromboli';
let pwd2 = 'stROmBoLi'

// Add your code here
let weightComparison = eleWeight < mouseWeight;
let heightComparison = ostrichHeight > duckHeight; 
let pwdMatch = pwd1 === pwd2;

// Don't edit the code below here!

section.innerHTML = ' ';
let para1 = document.createElement('p');
let para2 = document.createElement('p');
let para3 = document.createElement('p');

let weightTest = weightComparison ? 'True — elephants weight less than mice!?' : 'False — of course an elephant is heavier than a mouse!';
let heightTest = heightComparison ? 'True — an ostrich is indeed taller than a duck!' : 'False — apparently a duck is taller than an ostrich!?';
let pwdTest = pwdMatch ? 'True — the passwords match.' : 'False — the passwords do not match; please check them';

para1.textContent = weightTest;
section.appendChild(para1);
para2.textContent = heightTest;
section.appendChild(para2);
para3.textContent = pwdTest;
section.appendChild(para3);
    

阅读量: 634
发布于:
修改于: