Baseline Widely available
Math.floor()
彿°æ»æ¯è¿åå°äºçäºä¸ä¸ªç»å®æ°åçæå¤§æ´æ°ã
console.log(Math.floor(5.95));
// Expected output: 5
console.log(Math.floor(5.05));
// Expected output: 5
console.log(Math.floor(5));
// Expected output: 5
console.log(Math.floor(-5.05));
// Expected output: -6
è¯æ³ åæ° è¿åå¼
å°äºçäº x
çæå¤§æ´æ°ãå®çå¼ä¸ -Math.ceil(-x)
ç¸åã
å 为 floor()
æ¯ Math
çéææ¹æ³ï¼æä»¥ä½ åºå§ç»ä½¿ç¨ Math.floor()
ï¼è䏿¯ä½ä¸ºä½ å建ç Math
å¯¹è±¡çæ¹æ³ï¼Math
䏿¯æé 彿°ï¼ã
Math.floor(-Infinity); // -Infinity
Math.floor(-45.95); // -46
Math.floor(-45.05); // -46
Math.floor(-0); // -0
Math.floor(0); // 0
Math.floor(4); // 4
Math.floor(45.05); // 45
Math.floor(45.95); // 45
Math.floor(Infinity); // Infinity
åè¿å¶è°æ´
卿¬ä¾ä¸ï¼æä»¬å®ç°äºä¸ä¸ªå为 decimalAdjust()
çæ¹æ³ï¼å®æ¯ Math.floor()
ãMath.ceil()
å Math.round()
çå¢å¼ºæ¹æ³ãä¸ä¸ª Math
彿°æ»æ¯å°è¾å
¥è°æ´ä¸ºä¸ªä½æ°ï¼decimalAdjust
æ¥å exp
åæ°ï¼è¯¥åæ°æå®å°æ°ç¹å·¦ä¾§åºè¯¥è°æ´ç使°ãä¾å¦ï¼-1
表示å®å°å¨å°æ°ç¹åçä¸ä¸ä½æ°åï¼å¦ "à 10-1"ï¼ãæ¤å¤ï¼å®è¿å
è®¸ä½ éè¿ type
åæ°éæ©è°æ´æ¹å¼ââround
ãbottom
æ ceiling
ã
宿¯è¿æ ·åçï¼å°æ°åä¹ä»¥ 10 çå¹ï¼ç¶ååèäºå
¥å°ææ¥è¿çæ´æ°ï¼ç¶åé¤ä»¥ 10 çå¹ãä¸ºäºæ´å¥½å°ä¿æç²¾åº¦ï¼å®å©ç¨äºæ°åç toString()
æ¹æ³ï¼è¯¥æ¹æ³ä½¿ç¨ç§å¦è®°æ°æ³è¡¨ç¤ºä»»ææ°åï¼å¦ 6.02e23
ï¼ã
/**
* Adjusts a number to the specified digit.
*
* @param {"round" | "floor" | "ceil"} type The type of adjustment.
* @param {number} value The number.
* @param {number} exp The exponent (the 10 logarithm of the adjustment base).
* @returns {number} The adjusted value.
*/
function decimalAdjust(type, value, exp) {
type = String(type);
if (!["round", "floor", "ceil"].includes(type)) {
throw new TypeError(
"The type of decimal adjustment must be one of 'round', 'floor', or 'ceil'.",
);
}
exp = Number(exp);
value = Number(value);
if (exp % 1 !== 0 || Number.isNaN(value)) {
return NaN;
} else if (exp === 0) {
return Math[type](value);
}
const [magnitude, exponent = 0] = value.toString().split("e");
const adjustedValue = Math[type](`${magnitude}e${exponent - exp}`);
// Shift back
const [newMagnitude, newExponent = 0] = adjustedValue.toString().split("e");
return Number(`${newMagnitude}e${+newExponent + exp}`);
}
// Decimal round
const round10 = (value, exp) => decimalAdjust("round", value, exp);
// Decimal floor
const floor10 = (value, exp) => decimalAdjust("floor", value, exp);
// Decimal ceil
const ceil10 = (value, exp) => decimalAdjust("ceil", value, exp);
// Round
round10(55.55, -1); // 55.6
round10(55.549, -1); // 55.5
round10(55, 1); // 60
round10(54.9, 1); // 50
round10(-55.55, -1); // -55.5
round10(-55.551, -1); // -55.6
round10(-55, 1); // -50
round10(-55.1, 1); // -60
// Floor
floor10(55.59, -1); // 55.5
floor10(59, 1); // 50
floor10(-55.51, -1); // -55.6
floor10(-51, 1); // -60
// Ceil
ceil10(55.51, -1); // 55.6
ceil10(51, 1); // 60
ceil10(-55.59, -1); // -55.5
ceil10(-59, 1); // -50
è§è æµè§å¨å
¼å®¹æ§ åè§
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4