Temporal.Instant.prototype.round()
round() 方法是 Temporal.Instant 例項的一個方法,它返回一個新的 Temporal.Instant 物件,該物件表示將此時間戳舍入到指定單位後的結果。
語法
js
round(smallestUnit)
round(options)
引數
smallestUnit(最小單位)-
表示
smallestUnit選項的字串。這是一個便捷的過載,因此round(smallestUnit)等價於round({ smallestUnit }),其中smallestUnit是一個字串。 options-
一個包含以下部分或全部屬性的物件(按檢索和驗證的順序):
roundingIncrement可選-
一個(截斷為整數的)數字,表示給定
smallestUnit的舍入增量。預設為1。增量和smallestUnit必須能整除 24 小時;例如,45 秒是 86400 秒的除數,100 分鐘是 3600 分鐘的除數。這比其他類的round()方法的要求略微寬鬆,其他類要求增量是該單位最大值的除數。 roundingMode可選-
一個字串,指定如何對
smallestUnit的小數部分進行四捨五入。參見Intl.NumberFormat()。預設為"halfExpand"。 smallestUnit(最小單位)-
一個字串,表示輸出中包含的最小單位。該值必須是以下之一:
"hour"、"minute"、"second"、"millisecond"、"microsecond"、"nanosecond",或它們的複數形式。對於大於"nanosecond"的單位,smallestUnit的小數部分將根據roundingIncrement和roundingMode設定進行舍入。
返回值
一個 Temporal.Instant 新物件,表示將此時間戳舍入到指定單位後的結果,其中所有小於 smallestUnit 的單位都將被歸零。
異常
RangeError-
如果任何選項無效,則丟擲。
示例
小單位的四捨五入
js
const instant = Temporal.Instant.fromEpochMilliseconds(1000);
const roundedInstant = instant.round("second");
console.log(roundedInstant.epochMilliseconds); // 1000
const instant2 = instant.round("minute");
console.log(instant2.epochMilliseconds); // 0
規範
| 規範 |
|---|
| Temporal # sec-temporal.instant.prototype.round |
瀏覽器相容性
載入中…