激情综合色五月六月婷婷,久久久久久综合网天天,国产精品久久久久精品综合紧无码

  • 2022-10-27
宇哥博客 前端開發 js中的parseInt()和Number()詳解

js中的parseInt()和Number()詳解

parseInt() 函數:
如果參數是以“0x”開頭,parseInt() 會把其余部分以十六進制解析。

測試代碼:

<script type="text/javascript">
	var num1 = parseInt('123');		//結果:123
	var num2 = parseInt('');		//結果:NaN
	var num3 = parseInt('123.33');	//結果:123
	var num4 = parseInt('123abb');	//結果:123
	var num5 = parseInt('123.55abb');	//結果:123
	var num6 = parseInt(null);		//結果: NaN
	var num7 = parseInt(undefined);	//結果: NaN
	var num8 = parseInt('0x14');	//十六進制轉為了十進制,結果:20
	var num9 = parseInt('0x14', 16);//第2個參數表示進制,結果:20
	var num10 = parseInt('1101',2);	//結果:13
	var num11 = parseInt('asd123');	//結果: NaN
	var num12 = parseInt('asd');	//結果: NaN
	var num13 = parseInt(' 123 ');		//結果:123
	var num14 = parseInt(true);	//結果:NaN
	var num15 = parseInt(false);	//結果:NaN

	console.log('num1', num1);
	console.log('num2', num2);
	console.log('num3', num3);
	console.log('num4', num4);
	console.log('num5', num5);
	console.log('num6', num6);
	console.log('num7', num7);
	console.log('num8', num8);
	console.log('num9', num9);
	console.log('num10', num10);
	console.log('num11', num11);
	console.log('num12', num12);
	console.log('num13', num13);
	console.log('num14', num14);
	console.log('num15', num15);

	//-----------------------------------------

	var num100 = Number('123');		//結果:123
	var num101 = Number('');		//結果:0
	var num102 = Number('123.33');	//結果:123.33
	var num103 = Number('123abb');	//結果:NaN
	var num104 = Number('123.55abb');	//結果:NaN
	var num105 = Number(null);		//結果: 0
	var num106 = Number(undefined);	//結果: NaN
	var num107 = Number('0x14');	//結果:20
	var num108 = Number('asd123');	//結果: NaN
	var num109 = Number('asd');	//結果: NaN
	var num110 = Number(' 123 ');		//結果:123
	var num111 = Number(true);	//結果:1
	var num112 = Number(false);	//結果:0

	console.log('num100', num100);
	console.log('num101', num101);
	console.log('num102', num102);
	console.log('num103', num103);
	console.log('num104', num104);
	console.log('num105', num105);
	console.log('num106', num106);
	console.log('num107', num107);
	console.log('num108', num108);
	console.log('num109', num109);
	console.log('num110', num110);
	console.log('num111', num111);
	console.log('num112', num112);
</script>

運行截圖:

本文來自網絡,不代表本站立場,轉載請注明出處。http://www.callingbugs.com/2539.html

發表評論

您的電子郵箱地址不會被公開。

返回頂部
激情综合色五月六月婷婷,久久久久久综合网天天,国产精品久久久久精品综合紧无码