library-ts/browser/text/leadingZeros.ts

18 lines
336 B
TypeScript
Raw Permalink Normal View History

2025-03-08 12:22:18 +00:00
export function leadingZeros( value:number, numDigits:number = 2 )
{
if ( numDigits <= 1 )
{
return value;
}
let hasMinus = value < 0;
let stringValue = Math.abs( value ) + "";
while ( stringValue.length < numDigits )
{
stringValue = "0" + stringValue;
}
return ( hasMinus ? "-" : "" ) + stringValue;
}