File tree 2 files changed +11
-1
lines changed
2 files changed +11
-1
lines changed Original file line number Diff line number Diff line change @@ -139,7 +139,8 @@ class Duration {
139
139
140
140
let seconds = this . $d . seconds || 0
141
141
if ( this . $d . milliseconds ) {
142
- seconds += Math . round ( this . $d . milliseconds ) / 1000
142
+ seconds += this . $d . milliseconds / 1000
143
+ seconds = Math . round ( seconds * 1000 ) / 1000
143
144
}
144
145
145
146
const S = getNumberUnitFormat ( seconds , 'S' )
Original file line number Diff line number Diff line change @@ -77,6 +77,15 @@ describe('Creating', () => {
77
77
expect ( dayjs . duration ( 1000.5 ) . toISOString ( ) ) . toBe ( 'PT1.001S' )
78
78
expect ( dayjs . duration ( - 1000.5 ) . toISOString ( ) ) . toBe ( '-PT1S' )
79
79
} )
80
+ it ( 'should handle floating point rounding errors' , ( ) => {
81
+ // An example of this is when adding 2 to 0.812 seconds, which is how
82
+ // the seconds component is calculated in .toISOString().
83
+ // > 2 + 0.812
84
+ // 2.8120000000000003
85
+ expect ( dayjs . duration ( - 2812 ) . toISOString ( ) ) . toBe ( '-PT2.812S' ) // was -PT2.8120000000000003S
86
+ expect ( dayjs . duration ( 3121632.27382247 ) . toISOString ( ) ) . toBe ( 'PT52M1.632S' ) // was PT52M1.6320000000000001S
87
+ expect ( dayjs . duration ( 7647826.525774224 ) . toISOString ( ) ) . toBe ( 'PT2H7M27.827S' ) // was PT2H7M27.826999999999998S
88
+ } )
80
89
} )
81
90
82
91
describe ( 'Parse ISO string' , ( ) => {
You can’t perform that action at this time.
0 commit comments