fix(common): format fractional seconds (#24844)

fix a bug introduced in #24831

PR Close #24844
This commit is contained in:
Victor Berchet 2018-07-11 08:58:30 -07:00 committed by Miško Hevery
parent b9e095aa31
commit 0b4d85e9f1
2 changed files with 8 additions and 15 deletions

View File

@ -193,20 +193,9 @@ function padNumber(
return neg + strNum; return neg + strNum;
} }
/** function formatFractionalSeconds(milliseconds: number, digits: number): string {
* Trim a fractional part to `digits` number of digits. const strMs = padNumber(milliseconds, 3);
* Right pads with "0" to fit the requested number of digits if needed. return strMs.substr(0, digits);
*
* @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);
} }
/** /**
@ -226,7 +215,7 @@ function dateGetter(
part = 12; part = 12;
} }
} else if (name === DateType.FractionalSeconds) { } else if (name === DateType.FractionalSeconds) {
return trimRPadFractional(part, size); return formatFractionalSeconds(part, size);
} }
const localeMinus = getLocaleNumberSymbol(locale, NumberSymbol.MinusSign); const localeMinus = getLocaleNumberSymbol(locale, NumberSymbol.MinusSign);

View File

@ -319,6 +319,10 @@ describe('Format date', () => {
expect(formatDate(3000, 'm:ss.S', 'en')).toEqual('0:03.0'); 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.SS', 'en')).toEqual('0:03.00');
expect(formatDate(3000, 'm:ss.SSS', 'en')).toEqual('0:03.000'); 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');
}); });
}); });
}); });