Math.floor() 返回小于或等于一个给定数字的最大整数。
一个 数字 最大 返回 整数 等于 Math 给定
2023-09-11 14:22:03 时间
Math.floor()
返回小于或等于一个给定数字的最大整数。
Note: Math.floor() ===
向下取整
语法节
Math.floor(x)
参数节
x
一个数字。
返回值 节
一个表示小于或等于指定数字的最大整数的数字。
描述节
由于 floor
是 Math
的一个静态方法,你总是应该像这样使用它 Math.floor()
,而不是作为你创建的一个Math对象的一种方法(Math不是一个构造函数)。
示例节
例子:使用 Math.floor
节
Math.floor( 45.95);
// 45
Math.floor( 45.05);
// 45
Math.floor( 4 );
// 4
Math.floor(-45.05);
// -46
Math.floor(-45.95);
// -46
例子:十进制调整节
// Closure
(function(){
/**
* Decimal adjustment of a number.
*
* @param {String} type The type of adjustment.
* @param {Number} value The number.
* @param {Integer} exp The exponent (the 10 logarithm of the adjustment base).
* @returns {Number} The adjusted value.
*/
function decimalAdjust(type, value, exp) {
// If the exp is undefined or zero...
if (typeof exp === 'undefined' || +exp === 0) {
return Math[type](value);
}
value = +value;
exp = +exp;
// If the value is not a number or the exp is not an integer...
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
return NaN;
}
// Shift
value = value.toString().split('e');
value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
// Shift back
value = value.toString().split('e');
return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
}
// Decimal round
if (!Math.round10) {
Math.round10 = function(value, exp) {
return decimalAdjust('round', value, exp);
};
}
// Decimal floor
if (!Math.floor10) {
Math.floor10 = function(value, exp) {
return decimalAdjust('floor', value, exp);
};
}
// Decimal ceil
if (!Math.ceil10) {
Math.ceil10 = function(value, exp) {
return decimalAdjust('ceil', value, exp);
};
}
})();
// Round
Math.round10(55.55, -1); // 55.6
Math.round10(55.549, -1); // 55.5
Math.round10(55, 1); // 60
Math.round10(54.9, 1); // 50
Math.round10(-55.55, -1); // -55.5
Math.round10(-55.551, -1); // -55.6
Math.round10(-55, 1); // -50
Math.round10(-55.1, 1); // -60
// Floor
Math.floor10(55.59, -1); // 55.5
Math.floor10(59, 1); // 50
Math.floor10(-55.51, -1); // -55.6
Math.floor10(-51, 1); // -60
// Ceil
Math.ceil10(55.51, -1); // 55.6
Math.ceil10(51, 1); // 60
Math.ceil10(-55.59, -1); // -55.5
Math.ceil10(-59, 1); // -50
规范节
规范版本 | 规范状态 | 注解 |
---|---|---|
ECMAScript 1st Edition. Implemented in JavaScript 1.0 | Standard | Initial definition. |
ECMAScript 5.1 (ECMA-262) Math.floor |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) Math.floor |
Standard | |
ECMAScript Latest Draft (ECMA-262) Math.floor |
Draft |
浏览器兼容性节
We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
相关链接节
- The
Math
object it belongs to. Math.abs
Math.ceil()
Math.round()
Math.sign()
Math.trunc()
文档标签和贡献者
标签:
此页面的贡献者: mdnwebdocs-bot, wangyukai04, xgqfrms-GitHub, AlexChao
最后编辑者: mdnwebdocs-bot, Mar 18, 2019, 4:43:31 PM
相关文章
- 单调栈应用--将一个数删除n各数字之后的最大最小值
- 一个危险的XSS案例——轻松拿到登录用户的cookie
- 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字
- 关于如何构建一个微博型广播(转)
- Python实现按照指定要求逆序输出一个数字的方法
- 数据分析不是死板工作,这是一个“看数字说故事”的比赛!
- (笔试题)把一个整数数组中重复的数字去掉
- R语言 如何用IF语句判断 一个数字的奇偶
- LeetCode-1780. 判断一个数字是否可以表示成三的幂的和【数学】
- Redis 基本架构:一个键值数据库包含什么?
- 一个 SAP 开发工程师十余年的技术写作之路回顾
- 使用 outlet 在SAP Spartacus 的页面添加自定义 HTML 元素的一个例子
- 开发一个简单的工具,导出github仓库所有issue列表
- Atitit 代码的艺术 attilax 艾提拉著作 1. 代码就像一首歌,一个文章,一个绘画1 2. 代码就像文章2 2.1. ,要流畅读出来,使用dsl 方法连模式2 2.2. 段落划分与
- 通过一个实际的例子学习 combineLatest
- 习题 3.17 求Sn=a+aa+aaa+···+aa···a之值,其中a是一个数字。例如:2+22+222+2222+22222(此时n=5),n由键盘输入。
- 习题 10.1 定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和。
- 习题 7.9 编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格、和其他字符的个数,在主函数中输入字符串以及输出上述的结果。
- 习题 7.8 写一个函数,输入一个4位数字,要求输出这4个数字字符,但每两个数字间空一个空格。如输入1990,应输出“1 9 9 0”。
- 【大数据&AI人工智能】数字孪生:为城市和你创造一个虚拟副本
- Python 为什么sys.stdout.write 输出时后面总跟一个数字
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
- Sharepoint 2013 左右"SPChange"一个简短的引论
- 2022年数字IC设计,笔试要求设计一个三分频电路
- 算法战斗:给定一个号码与通配符问号W,问号代表一个随机数字。 给定的整数,得到X,和W它具有相同的长度。 问:多少整数协议W的形式和的比率X大?
- 【第12天】给定一个X进制数字A,请你把它转换为十进制打印 | 进制转换
- QT之一个UI里边多界面切换