fix(common): format fractional seconds (#24844)
fix a bug introduced in #24831 PR Close #24844
This commit is contained in:
parent
b9e095aa31
commit
0b4d85e9f1
|
@ -193,20 +193,9 @@ function padNumber(
|
|||
return neg + strNum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trim a fractional part to `digits` number of digits.
|
||||
* Right pads with "0" to fit the requested number of digits if needed.
|
||||
*
|
||||
* @param num The fractional part value
|
||||
* @param digits The width of the output
|
||||
*/
|
||||
function trimRPadFractional(num: number, digits: number): string {
|
||||
let strNum = String(num);
|
||||
// Add padding at the end
|
||||
while (strNum.length < digits) {
|
||||
strNum = strNum + 0;
|
||||
}
|
||||
return strNum.substr(0, digits);
|
||||
function formatFractionalSeconds(milliseconds: number, digits: number): string {
|
||||
const strMs = padNumber(milliseconds, 3);
|
||||
return strMs.substr(0, digits);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -226,7 +215,7 @@ function dateGetter(
|
|||
part = 12;
|
||||
}
|
||||
} else if (name === DateType.FractionalSeconds) {
|
||||
return trimRPadFractional(part, size);
|
||||
return formatFractionalSeconds(part, size);
|
||||
}
|
||||
|
||||
const localeMinus = getLocaleNumberSymbol(locale, NumberSymbol.MinusSign);
|
||||
|
|
|
@ -319,6 +319,10 @@ describe('Format date', () => {
|
|||
expect(formatDate(3000, 'm:ss.S', 'en')).toEqual('0:03.0');
|
||||
expect(formatDate(3000, 'm:ss.SS', 'en')).toEqual('0:03.00');
|
||||
expect(formatDate(3000, 'm:ss.SSS', 'en')).toEqual('0:03.000');
|
||||
expect(formatDate(3001, 'm:ss', 'en')).toEqual('0:03');
|
||||
expect(formatDate(3001, 'm:ss.S', 'en')).toEqual('0:03.0');
|
||||
expect(formatDate(3001, 'm:ss.SS', 'en')).toEqual('0:03.00');
|
||||
expect(formatDate(3001, 'm:ss.SSS', 'en')).toEqual('0:03.001');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue