function strpad(input, size, right, prefix) {
//o array gera os zeros para usar na frente ou depois
var ps = Array(size).join(prefix ? prefix : "0");
//Inverte para o sufixo
if (right) {
return (input + ps).slice(0, size);
} else {
return (ps + input).slice(-size);
}
}
//3 prefixos
console.log(strpad('1', 3));
console.log(strpad('22', 3));
console.log(strpad('333', 3));
console.log(strpad('4444', 3));
console.log("-----------");
//4 prefixos
console.log(strpad('1', 4));
console.log(strpad('22', 4));
console.log(strpad('333', 4));
console.log(strpad('4444', 4));
console.log("-----------");
//3 sufixos
console.log(strpad('1', 3, true));
console.log(strpad('22', 3, true));
console.log(strpad('333', 3, true));
console.log(strpad('4444', 3, true));
console.log("-----------");
//4 sufixos
console.log(strpad('1', 4, true));
console.log(strpad('22', 4, true));
console.log(strpad('333', 4, true));
console.log(strpad('4444', 4, true));
console.log("-----------");
//Prefixo customizado
console.log(strpad('1', 4, false, 'B'));
console.log(strpad('22', 6, false, 'C'));
console.log(strpad('333', 8, false, 'Y'));
console.log(strpad('4444', 9, false, 'Z'));
Direct via mysql do so
– rray
@rray The problem with your answer method is that it wants to put two zeros on the left regardless of the size of the ID, which you suggested to complete with zeros until you get a specific number of digits
– Mateus
@bfavaretto had not even noticed that he had for Js :/ should have answered there, although it varies a little, since there seems to limit the string, but overall it should be the same thing.
– Guilherme Nascimento