[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Math/floor [Back]  [Original]

Math.floor() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Math.floor()

Baseline

20157

Math.floor()

console.log(Math.floor(5.95));
// : 5

console.log(Math.floor(5.05));
// : 5

console.log(Math.floor(5));
// : 5

console.log(Math.floor(-5.05));
// : -6

js
Math.floor(x)

x

x -Math.ceil(-x)

floor() Math Math Math.floor() (Math )

Math.floor()

js
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 3 decimalAdjust exp -1 1 " 10-1" roundfloorceil type

10 10 Number toString() 6.02e23

js
/**
 * 
 *
 * @param {"round" | "floor" | "ceil"} type 
 * @param {number} value 
 * @param {number} exp 10
 * @returns {number} 
 */
function decimalAdjust(type, value, exp) {
  type = String(type);
  if (!["round", "floor", "ceil"].includes(type)) {
    throw new TypeError(
      "'round''floor''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${Number(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

ECMAScript 2027 LanguageSpecification
# sec-math.floor


Web Proxy Viewer  |  New URL  |  Original Page