Temporal.PlainTime.prototype.round()
round() 方法是 Temporal.PlainTime 实例,它返回一个新的 Temporal.PlainTime 对象,表示将此时间四舍五入到给定单位。
语法
js
round(smallestUnit)
round(options)
参数
smallestUnit(最小单位)-
表示
smallestUnit选项的字符串。这是一个便捷的重载,因此round(smallestUnit)等价于round({ smallestUnit }),其中smallestUnit是一个字符串。 options-
一个包含以下部分或全部属性的对象(按检索和验证的顺序):
roundingIncrement可选-
一个数字(截断为整数),表示给定
smallestUnit的舍入增量。默认为1。增量必须是smallestUnit最大值的除数;例如,如果单位是小时,增量必须是 24 的除数,并且不能是 24 本身,这意味着它可以是 1、2、3、4、6、8 或 12。 roundingMode可选-
一个字符串,指定如何对
smallestUnit的小数部分进行四舍五入。参见Intl.NumberFormat()。默认为"halfExpand"。 smallestUnit(最小单位)-
一个字符串,表示输出中包含的最小单位。该值必须是以下之一:
"hour"、"minute"、"second"、"millisecond"、"microsecond"、"nanosecond"或它们的复数形式。对于大于"nanosecond"的单位,smallestUnit的小数部分将根据roundingIncrement和roundingMode设置进行四舍五入。
返回值
一个新的 Temporal.PlainTime 对象,表示将此时间四舍五入到给定单位,其中所有小于 smallestUnit 的单位都将归零。
异常
RangeError-
如果任何选项无效,则抛出。
示例
小单位的四舍五入
js
const time = Temporal.PlainTime.from("12:34:56.123456789");
const nearestMillisecond = time.round("millisecond");
console.log(nearestMillisecond.toString()); // 12:34:56.123
const nearestHalfHour = time.round({
smallestUnit: "minute",
roundingIncrement: 30,
});
console.log(nearestHalfHour.toString()); // 12:30:00
const nextHour = time.round({ smallestUnit: "hour", roundingMode: "ceil" });
console.log(nextHour.toString()); // 13:00:00
规范
| 规范 |
|---|
| Temporal # sec-temporal.plaintime.prototype.round |
浏览器兼容性
加载中…