JS資料型別 數字型別

2021-10-10 12:44:08 字數 1957 閱讀 2553

捨入(rounding)是使用數字時最常用的操作之一。

將數字捨入到小數點後n位

let num = 1.232312  

let a = math.floor(num*100)/100

console.log( a );

let num = 12.34

console.log(num.tofied(1)); // "12.3

ps:tofixed(n)返回的是乙個字串、不足部分添零補足;可以使用一元加號或者numer()呼叫,將其轉換為數字:+num.tofixed(n)

當我們對兩個數字進行求和時,它們的『精度損失』就會疊加,就好比為何0.1+.02 != 0.3

let sum = 0.1 + 0.2

console.log(sum.tofixed(2)); //「0.30」

ps:: tofixed(n): 返回的是字串需要用一元加號強轉為數字

let sum = 0.1 + 0.2

console.log(+sum.tofixed(2));

console.log(isnan(nan)); // true

console.log(isnan("str)); // true

console.log(isfinite("12")); // true

console.log(isfinite("str"));//false--->特殊值:nan

console.log(isfinite(infinity)); // false,--->特殊值:infinify

ps: 有時isfinite被驗證字串是否為常規數字,在所有數字函式中,包括isfinite,空字串或僅有空格字串均被視為0

object.is()方法是判斷兩個值是否 相同的值 ,這種相等潘頓邏輯與以往== 的判斷不同,==運算子會對它兩邊的運算元做隱式類轉換若型別不同才進行相等性比較。

object.is('foo', 'foo');     // true

object.is(window, window); // true

object.is('foo', 'bar'); // false

object.is(, ); // false

var test = ;

object.is(test, test); // true

object.is(null, null); // true

// 特例

object.is(0, -0); // false

object.is(-0, -0); // true

object.is(nan, 0/0); // true

可以在字串中讀取 數字,直到無法讀取為止,若發生error,則返**集到的數字

alert( parseint('100px') ); // 100

alert( parsefloat('12.5em') ); // 12.5

alert( parseint('12.3') ); // 12,只有整數部分被返回了

alert( parsefloat('12.3.4') ); // 12.3,在第二個點出停止了讀取

而某些情況下,parseint/parsefloat 會返回nan,當沒有數字可讀時會發生如下情況

console.log(parseint('a123')); //nan,第乙個符號就停止讀取

js資料型別

一.原始資料型別 1.typeof 運算子。var a alert typeof a 輸出結果為 undefined 2.undefined 未定義 型別 當變數未定義時,該變數值被預設為undefined 如 var a alert typeof a 輸出結果為 undefined 注意值unde...

js資料型別

js 基本資料型別 undefined null boolean number string js 操作符 typeof檢測給定變數的資料型別。返回值表示的型別 undefined 值未定義。boolean 值為布林型別。string 值為字串。number 值為數字。object 值為物件或nul...

js資料型別

var num 1,boo true,aa null,bb,str mary arr 1,2,4,8 obj arrnew new array 1,2,3 strnew new string 1,2,3 用 typeof 檢測變數的型別 console.log number typeof num n...