详解js中Number,parseInt和parseFloat的区别 详解js中Number,parseInt和parseFloa...
Javascript\u4e2dNumber\uff0cparseIn\u548cparseFloat\u7684\u533a\u522b\u4e0e parseInt()\u51fd\u6570\u7c7b\u4f3c\uff0cparseFloat()\u4e5f\u662f\u4ece\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff08\u4f4d\u7f6e0\uff09\u5f00\u59cb\u89e3\u6790\u6bcf\u4e2a\u5b57\u7b26\u3002\u800c\u4e14\u4e5f\u662f\u4e00\u76f4\u89e3\u6790\u5230\u5b57\u7b26\u4e32\u672b\u5c3e\uff0c\u6216\u8005\u89e3\u6790\u5230\u9047\u89c1\u4e00 \u4e2a\u65e0\u6548\u7684\u6d6e\u70b9\u6570\u5b57\u5b57\u7b26\u4e3a\u6b62\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5c0f\u6570\u70b9\u662f\u6709\u6548\u7684\uff0c\u800c\u7b2c\u4e8c\u4e2a\u5c0f\u6570\u70b9\u662f\u65e0\u6548\u7684\uff0c\u56e0\u6b64\u5b83\u540e\u9762\u7684\u5b57\u7b26\u4e32\u5c06\u88ab\u5ffd\u7565\u3002\u4e3e\u4f8b\u6765 \u8bf4\uff0c\u201c22.34.5\u201d\u5c06\u4f1a\u88ab\u8f6c\u6362\u4e3a22.34\u3002
\u9664\u4e86\u7b2c\u4e00\u4e2a\u5c0f\u6570\u70b9\u6709\u6548\u4e4b\u5916\uff0cparseFloat()\u4e0eparseInt()\u7684\u7b2c\u4e8c\u4e2a\u533a\u522b \u5728\u4e8e\u5b83\u59cb\u7ec8\u90fd\u4f1a\u5ffd\u7565\u524d\u5bfc\u96f6\u3002parseFloat()\u53ef\u4ee5\u8bc6\u522b\u524d\u9762\u8ba8\u8bba\u8fc7\u7684\u6240\u6709\u6d6e\u70b9\u6570\u503c\u683c\u5f0f\uff0c\u4e5f\u5305\u62ec\u5341\u8fdb\u5236\u6574\u6570\u683c\u5f0f\u3002\u4f46\u5341\u516d\u8fdb\u5236\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u59cb\u7ec8\u4f1a\u88ab\u8f6c\u6362 \u4e3a0\u3002\u7531\u4e8eparseFloat()\u53ea\u89e3\u6790\u5341\u8fdb\u5236\u503c\uff0c\u56e0\u6b64\u5b83\u6ca1\u6709\u7528\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u5b9a\u57fa\u6570\u7684\u7528\u6cd5\u3002
Number\u662f\u5c06\u51fd\u6570\u628a\u5bf9\u8c61\u8f6c\u6362\u6210\u6570\u5b57\uff1b
ParseInt\u53ef\u4ee5\u505a\u8fdb\u5236\u8f6c\u6362\uff08\u5b57\u7b26\u524d\u52a00x\u6216\u9650\u5b9a\u4ed6\u662f16\u8fdb\u5c3116\u8fdb\u5236\uff0c\u4e0d\u7ec6\u8bb2\uff09;\u5f53\u7136\u4ed6\u8fd8\u6709\u4e00\u4e2a\u4f5c\u7528\u5c31\u662f\u53d6\u6574\u6570\u90e8\u5206(\u5b57\u7b26\u4e32\u5185\u7b2c\u4e00\u4e2a\u975e\u6570\u5b57\u4ee5\u524d\u7684\u6240\u6709\u5185\u5bb9\u3010\u5305\u62ec\u5c0f\u6570\u70b9\u3011)\u3002
parseFloat\u548cparseInt\u5f88\u50cf\uff0c\u4e0d\u8fc7\u4ed6\u53d6\u7684\u662f\u6d6e\u70b9\u6570\uff0c\u4e5f\u5c31\u662f\u78b0\u5230\u5c0f\u6570\u70b9\u540e\u4e0d\u505c\u4e0b\uff0c\u7ee7\u7eed\u627e\u3002\u77e5\u9053\u78b0\u5230\u4e0b\u4e00\u4e2a\u5b57\u7b26
var a="0.5";var n=Number(a);//n=0.5var b=parseInt(a);//b=0var c=parseFloat(a);//c=0.5//\u533a\u522bvar _a="0.16e5s"var _b=Number(A);//_b=NaNvar _c=parseInt(_a);//_c=0var _d=parseFloat(_a);//d=0.16
Number把空字符串转换为0,而parseInt和parseFloat则是转换为NaN;
Number和parseInt会把有效的十六进制格式字符串(例如"0xf")转换为十进制数,而parseFloat会转为0;
Number和parseFloat都会把前面带0的数字字符串(如"0123")转换为十进制的整数或浮点数,而parseInt则按八进制数来转换(除非数字超出八进制的范围,如089);
Number和parseFloat会有效解析浮点数(比如"123.45mn"转为123.45),而parseInt只提取数字字符串中的整数部分(比如"123.45mn"转为123);
parseInt含有第二个参数,用来规定数字转换的进制数(从2到36),比如parseInt("10",12)会按十二进制来解析10,结果就是十进制的12。而Number和parseFloat则没有这个功能。
绛旓細璇︾粏姝ラ濡備笅锛1锛氬畾涔夌埗绫籪unctionParent(){}2锛氬畾涔夊瓙绫籪untion Son(){}3锛氶氳繃鍘熷瀷缁橭bject瀵硅薄娣诲姞涓涓墿灞曟柟娉曘侽bject.prototype.customExtend =function(parObj){for(variinparObj){//閫氳繃for-in寰幆锛屾妸鐖剁被鐨鎵鏈夊睘鎬ф柟娉曪紝璧嬪肩粰鑷繁this[i] =parObj[i];}}4锛...
绛旓細function dom() {var s= document.getElementByIdx_x("test");del_ff(s); //娓呯悊绌烘牸var chils= s.childNodes; //寰楀埌s鐨鍏ㄩ儴瀛愯妭鐐箆ar par=s.parentNode; //寰楀埌s鐨勭埗鑺傜偣var ns=s.nextSbiling; //鑾峰緱s鐨勪笅涓涓厔寮熻妭鐐箆ar ps=s.previousSbiling; //寰楀埌s鐨勪笂涓涓厔寮熻妭...
绛旓細return false;} console.log("濡傛灉par鏄1鐨勮瘽锛岃繖閲屽氨涓嶆墽琛屼簡")} test(0);test(1);绠鍗曠殑鏉ヨ灏辨槸 闈 鐨勬剰鎬濄
绛旓細arg1 = Number(arg1.toString().replace(".", ""));arg2 = Number(arg2.toString().replace(".", ""));} return (arg1 + arg2) / m;}//缁橬umber绫诲瀷澧炲姞涓涓猘dd鏂规硶锛岃皟鐢ㄨ捣鏉ユ洿鍔犳柟渚裤侼umber.prototype.add = function (arg) { return accAdd(arg, this);};...
绛旓細function ConvertDate(timestr, format) // par1: 1255574329, par2: 'Y-m-d'{ var time=new Date(timestr+'000');result=time.format(format);// 鍏蜂綋瀹炵幇缃戜笂寰堝 return result; // 2009-10-15 } 0
绛旓細浣跨敤JSON鏍煎紡鐨勮娉曞嵆鐩存帴浣跨敤濡備笅璇硶瀹氫箟鏁扮粍锛歔parrm,param,param...],鑰屼笉鏄噰鐢╪ew Array(parrm,param,param...)杩欑璇硶銆傚洜涓轰娇鐢↗SON鏍煎紡鐨勮娉曟槸寮曟搸鐩存帴瑙i噴鐨勩傝屽悗鑰呭垯闇瑕佽皟鐢ˋrray鐨勬瀯閫犲櫒銆傚崄浜屻 瀵瑰瓧绗︿覆杩涜寰幆鎿嶄綔锛屼緥濡傛浛鎹佹煡鎵撅紝灏变娇鐢ㄦ鍒欒〃杈惧紡銆傚洜涓JS鐨寰幆閫熷害姣旇緝鎱紝鑰...
绛旓細鎬濊矾锛氫娇鐢╲alue灞炴ц幏鍙栧墠涓や釜鏂囨湰妗鐨鍊硷紝杞崲涓烘暟瀛楃浉鍔犲悗锛屼娇鐢╲alue灞炴ц缃涓変釜鏂囨湰妗嗙殑鍊笺傜ず渚嬪涓嬶細1銆丠TML缁撴瀯 +=2銆乯avascript浠g爜 window.onload = function(){var inputs = document.getElementsByTagName("input");for(i=0;i
绛旓細N 淇濇姢鎺ュ湴 PE 淇濇姢鎺ュ湴鍜屼腑鎬х嚎 PEN 宸姩 D 涓嶆帴鍦颁繚鎶 PU 鐩存祦 DC 鎺ュ湴 E 澶囩敤 RES 鏃犲櫔澹版帴鍦 TE 涓棿绾 M 鐢垫祦琛 PA 鐢靛帇琛 PV 鏈夊姛鐢靛害琛 PJ 鏃犲姛鐢靛害琛 PJR 棰戠巼琛 PF 鐩镐綅琛 PPA 鏈澶ч渶閲忚〃(璐熻嵎鐩戞帶浠) PM 鍔熺巼鍥犳暟琛 PPF 鏈夊姛鍔熺巼琛 PW 鏃犲姛鍔熺巼琛 PR 鏃犲姛鐢垫祦琛 PAR 澹...
绛旓細TSetProperty(movie_clip,property,number)鈥斺斺-璁剧疆movie_clip鐨勬寚瀹氬睘鎬 鍦ㄨ繖閲屾垜浠彧闇瑕佷娇鐢ㄧ殑鏄矖浣撴爣璇嗙殑SetVariable鏂规硶锛孞S閫氳繃璋冪敤姝ゆ柟娉曡兘澶熺洿鎺ユ洿鏀筍WF涓殑鍙橀噺鍊笺傞鍏堟垜浠渶瑕佸畾涔夋彃鍏ョ殑SWF鐨処D锛屼緥濡俰d涓篤skyDemo锛岄偅涔堟垜浠彲浠ラ氳繃涓嬮潰鐨凧S璇彞鏉ュ畬鎴怱WF鍐呴儴鍙橀噺鐨勮缃細 window.document....
绛旓細 /** * 鐢变簬鍘熺敓 js 鎿嶄綔 ByTagName 姣旇緝涓嶆柟渚匡紝鎵浠ュ缓璁氳繃 id 閫夋嫨鍣ㄦ潵瀹屾垚鎿嶄綔 * 棣栧厛鍙互涓烘瘡涓涓 a 鏍囩娣诲姞 id锛宨d = "mylink_*" * 鐒跺悗鍦 sel 鏂规硶涓紝浣跨敤涓や釜鍙傛暟锛屼竴涓槸瀵瑰簲鐨 a 鐨 mylink_* 涓殑 *锛屽彟澶栦竴涓氨鏄叾涓殑绗嚑鍓 img * 鏍规嵁 a 鏍囩鐨...