銆愮1913鏈熴€慚ath.sign锛氬湪Javascript涓浣曟鏌ヤ竴涓暟瀛楁槸姝f暟杩樻槸璐熸暟
Posted 鍓嶇鏃╄璇?/a> 銆愮
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆愮1913鏈熴€慚ath.sign锛氬湪Javascript涓浣曟鏌ヤ竴涓暟瀛楁槸姝f暟杩樻槸璐熸暟相关的知识,希望对你有一定的参考价值。
鍓嶈█
浣犺緭鍏ョ殑瓒婂锛屼綘鐨勮В鍐抽棶棰樼殑宸ュ叿绠卞氨瓒婁赴瀵屻€備粖鏃ユ棭璇绘枃绔犵敱@鎺掗缈昏瘧鎶曠锛孏itlab@samanthaming鎺堟潈鍒嗕韩銆?/p>
姝f枃浠庤繖寮€濮嬶綖锝?/p>
鐜板湪鏈変簡ES6鐨凪ath.sign 馃憦锛屽垽鏂竴涓暟瀛楃殑灏卞彉寰楄秴绾х畝鍗曚簡锛屽畠鍙互鏄剧ず鍑烘暟瀛楁槸姝f暟銆佽礋鏁拌繕鏄浂銆?/p>
const positive = 5;
const negative = -5;
const zero = 0;
Math.sign(positive); // 1
Math.sign(negative); // -1
Math.sign(zero); // 0
Math.sign 杩斿洖鍊?/h4>
Math.sign()鏈?涓彲鑳界殑杩斿洖鍊笺€?/p>
1 // positive number
-1 // negative number
0 // positive zero
-0 // negative zero
NaN // not a number
Math.sign(8); // 1
Math.sign(-8); // -1
Math.sign(0); // 0
Math.sign(-0); // -0
Math.sign(NaN); // NaN
Math.sign('hello'); // NaN
Math.sign(); //NaN
娉ㄦ剰锛屼紶閫掔粰杩欎釜鍑芥暟鐨勫弬鏁板皢琚殣寮忚浆鎹负鏁板瓧绫诲瀷銆?/p>
Math.sign Gotcha
涓€涓父瑙佺殑璇尯鏄涓篗ath.sign杩斿洖杞崲鍚庣殑鍙傛暟鍊笺€侻ath.sign鍙繑鍥炰竴涓暟瀛楃殑绗﹀彿銆傚畠骞朵笉杩斿洖鍊笺€?/p>
Math.sign(-8);
// 鉁?return -1
// 鉂?It doesn't return -8
Math.sign vs 姣旇緝
绀惧尯閲屾彁鍑轰簡涓€涓緢濂界殑闂锛?/p>
鏃㈢劧鍙互鐢ㄦ瘮杈冭繍绠楃锛屼负浠€涔堣繕瑕佺敤Math.sign锛?/p>
if (number > 0) {
// Positive
} else {
// Negative
}
versus
if (Math.sign(number) > 0) {
// Positive
} else {
// Negative
}
瀹為檯涓婏紝濡傛灉浣犲彧鏄鏌ュ竷灏旂姸鎬侊紝閭d箞鎴戝彧闇€瑕佷娇鐢ㄦ瘮杈冭繍绠楃锛岃€屼笉鏄娇鐢∕ath.sign銆備絾Math.sign鐨勯棯鍏夌偣鍦ㄤ簬瀹冭繑鍥炰竴涓暟瀛楀€笺€傝繖鎰忓懗鐫€浣犲彲浠ヨ繘琛岃绠椼€?/p>
const number = 5;
number > 0; // true
Math.sign(number); // 1
鐢∕ath.sign瑙e喅涓€涓畻娉曟寫鎴?/h4>
鎵€浠ワ紝瀹冭鎴戣В鍐充簡杩欎釜绠楁硶鎸戞垬锛?鍙嶈浆涓€涓暣鏁?
Input: 123;
Output: 321;
Input: -123;
Output: -321;
function reverseInteger(num) {
const numArray = Math.abs(num) // Get the absolute value of our number > 321
.toString() // Convert our number to a string > '321'
.split('') // Convert our string of numbers to an array > [3, 2, 1]
.reverse() // Reverse our array of numbers > [1, 2, 3]
.join(''); // Convert our array back to a string > 123
const sign = Math.sign(num); // -1
return numArray * sign;
// Multiply our reverse string with the sign will produce the correct reverse number
}
reverseInteger(-321); // -123
杩欓亾绠楁硶棰樻潵鑷簬Leetcode鐨勩€婂弽杞暣鏁般€嬨€備负浜嗙畝鍖栨垜浠殑婕旂ず锛屾垜鎶婅繖閬撻鐨勮姹傜紪杈戜簡涓€涓嬨€傝鐪嬪疄闄呯殑瑙f硶锛岃繖閲屾湁涓€閬撴潵鑷狜loia5tqd001銆?/p>
杩欏叾瀹炴槸鎴戠涓€娆″彂鐜拌繖涓嚱鏁般€傝繖涔熸槸鎴戝枩娆㈢湅鍒汉鐨勮В鍐虫柟妗堢殑鍘熷洜銆傜湅鍒汉鏄€庝箞瑙e喅鐨勶紝鎬绘槸寰堟湁鎰忔€濄€傚嵆浣胯В鍐虫柟娉曚笉濂斤紝鎴戜篃浼氱湅閭d簺锛屽洜涓哄畠鏁欐垜濡備綍閬垮厤馃槈銆傛病鏈夌煡璇嗘槸娴垂鐨勷煉€傛墍鏈夎繖浜涢兘鏄墿灞曟垜鐨勫伐鍏风銆傚氨鍍忛偅浜涙満鍣ㄥ涔犱竴鏍凤紝浣犺緭鍏ョ殑鏁版嵁瓒婂锛屽畠灏辫秺濂姐€傛垜鎯虫垜鐨勫ぇ鑴戜篃鏄繖鏍风殑銆傛垜闇€瑕佺湅鍒板緢澶氳В鍐虫柟妗堬紝鎵嶈兘璁╂垜鍙樺緱鏇村ソ馃槃馃槃
杩欏氨鏄负浠€涔堝湪鎴戠殑寰堝鑺辩诞涓紝鎴戦兘浼氭秹鍙婂埌涓嶅悓鐨勮В棰樻柟娉曘€傚洜涓烘案杩滄病鏈変竴涓渶濂界殑鏂规硶銆傛渶濂界殑鏂规硶鎬绘槸瑕佺湅鎯呭喌鑰屽畾銆備綘鐨勫伐鍏风瓒婂ぇ锛屼綘灏辫秺鏈夊彲鑳芥壘鍒版渶濂界殑鏂规硶馃憤銆?/p>
璐熼浂
鎵€浠ワ紝浣犲彲鑳戒細娉ㄦ剰鍒癕ath.sign杩斿洖鐨勬槸璐熸暟0銆?/p>
Math.sign(0); // 0
Math.sign(-0); // -0
鑰屼綘鐨勪笅涓€涓棶棰樻槸锛岃繖涓礋闆跺埌搴曟槸浠€涔堬紵鍦ㄤ綘涓嶆噦JS鐨凨yle Simpson瑙i噴寰楁渶娓呮銆?/p>
鐜板湪锛岄櫎浜嗗鏈笂鐨勭悙浜嬶紝鎴戜滑涓轰粈涔堣鐢ㄨ礋闆跺憿锛?/p>
鍦ㄦ煇浜涘簲鐢ㄤ腑锛屽紑鍙戜汉鍛樼敤涓€涓€肩殑澶у皬鏉ヨ〃绀轰竴涓俊鎭紙姣斿姣忓抚鍔ㄧ敾鐨勮繍鍔ㄩ€熷害锛夛紝骞朵娇鐢ㄨ繖涓暟瀛楃殑绗﹀彿鏉ヨ〃绀哄彟涓€涓俊鎭紙姣斿杩愬姩鐨勬柟鍚戯級銆?/p>
鍦ㄨ繖浜涘簲鐢ㄤ腑锛屼妇涓€涓緥瀛愶紝濡傛灉涓€涓彉閲忓埌杈鹃浂鐐规椂灏卞け鍘讳簡绗﹀彿锛岄偅涔堝湪瀹冨埌杈鹃浂鐐逛箣鍓嶏紝浣犲氨浼氬け鍘诲畠绉诲姩鏂瑰悜鐨勪俊鎭€備繚鐣欓浂鐨勭鍙峰彲浠ラ槻姝㈡綔鍦ㄧ殑涓嶉渶瑕佺殑淇℃伅涓㈠け銆?/p>
Math.sign娴忚鍣ㄦ敮鎸?/h4>
瀵规墍鏈夌幇浠g殑鏀寔閮藉緢涓嶉敊銆傚彲鎯滅殑鏄紝Internet Explorers澶椂楂︿簡锛屼笉鑳借窡鍏朵粬浜轰竴璧风帺鑰嶏紝鎵€浠ユ病鏈夋敮鎸侀偅閲屻€?/p>
IE鐨勪唬鐮佽姳绲?/h4>
浣嗕笉鐢ㄦ媴蹇冿紝杩欓噷鏈変竴涓唬鐮佺墖娈靛彲渚涢€夋嫨銆傝繖娈典唬鐮佸彲浠ュ湪IE娴忚鍣ㄥ拰鑰佺増鏈殑娴忚鍣ㄤ笂浣跨敤馃憤銆?/p>
const positive = 5;
const negative = -5;
const zero = 0;
positive === 0 ? positive : positive > 0 ? 1 : -1; // 1
negative === 0 ? negative : negative > 0 ? 1 : -1; // -1
zero === 0 ? zero : zero > 0 ? 1 : -1; // 0
Math.sign Polyfill
鎴栬€呯户缁娇鐢∕ath.sign锛屽彧闇€浠嶮DN涓坊鍔犺繖涓狿olyfill灏卞彲浠ヤ簡
f (!Math.sign) {
Math.sign = function(x) {
return (x > 0) - (x < 0) || +x;
};
}
璧勬簮
MDN Web Docs - Math.sign()
Stack Overflow: Number sign in javascript
Leetcode Solution: Reverse an integer
Math.sign MDN Polyfill
Stack Overflow: How to check the value given is a positive or negative integer?
Stack Overflow: JS Negative Number
涓轰綘鎺ㄨ崘
以上是关于銆愮1913鏈熴€慚ath.sign锛氬湪Javascript涓浣曟鏌ヤ竴涓暟瀛楁槸姝f暟杩樻槸璐熸暟的主要内容,如果未能解决你的问题,请参考以下文章
鏂囨。澶у笀 2016 锛氬湪鐢佃剳鍜屽ご鑴戜腑蹇€熸壘鍒版枃妗g殑鏂囦欢绠$悊杞欢
251.Spring Boot+Spring Security锛氬熀浜嶶RL鍔ㄦ€佹潈闄愶細鑷畾涔塅ilter