Complete numbers with Javascript

Asked

Viewed 181 times

-3

I’m stuck on a question here and I need a help.

I need to make an autocomplete in a boleto input.

Example:

12345.56789.00085.111111.11111.234566.6.0000000000

In this last field, if the user does not type anything he would have to autocomplete with "0".

If this were the case:

12345.56789.00085.111111.11111.234566.6.123

The input would have to return:

12345.56789.00085.111111.11111.234566.6.12300000000000

Always autocomplete with zeros to the right.

How can I do this in javascript?

  • Post part of your code so we can help you better.

  • 1

    Is there a pattern that there? When zero numbers on the right? Do the points continue as points? I think that there will need a mask, nor .repeat(14-num.length) and neither .padEnd(54, "0") will solve your real problem (I think, not sure). Please edit the question and make these points more clear.

  • In the case in my code already has this rule of allocating the "." to separate and yes is in the last field, in the case 14 digits. The problem that users sometimes forget to put the number zero, invalidating the ticket. Then it is to autocomplete until the required amount of character is: 54

4 answers

1

You can use the method repeat javascript to repeat the "0" n times according to the size of the string.
From what I saw in your example, you must have a size of 14 (the last part), so:

var num = "123";
console.log(num + "0".repeat(14-num.length));

1

The action of completing with a certain character until making a size is often called pad. Javascript already has functions to do pad both on the left and on the right, they being the padStart and padEnd respectively.

In your case using the padEnd has the intended effect:

let boleto = "12345.56789.00085.111111.11111.234566.6.123";
let boletoCompletado = boleto.padEnd(54, "0");
console.log(boletoCompletado);

The first parameter of padEnd, the 54, indicate the amount of careters you should have, and the second parameter indicates the character to place until you make the desired size.

If you need to support very old browsers where these two functions may not exist, you can use the polyfill mentioned in the documentation pages.

0

Simply do the . length of the string, and at the end if it is less than the desired number, for example:

if(input.length < 54)

makes += the number of 0s that can be done with a cycle is 54 - input.length (if 54 is the total number)

An example could be: (code may not compile, I’m not testing)

var textoInput = "";
(...)
if(textoInput.length < 54) {
   var zeros = "";
   for(int i = 0; i < (54 - TextoInpit.length); i++) {
       zeros += "0";
   }
   textoInput += zeros;
}

0

You can do something like this, but you need to calculate the amount you want to put in zeros, and the amount you want to limit:

var numero="12345.56789.00085.111111.11111.234566.6.123", numero_repeticoes = numero.length, numero_remocoes = numero.length * 2;

var content = "0".repeat(numero_repeticoes).padStart(numero_remocoes, numero); (content).substring(54, -54);

Or simplify like this:

numero.padEnd(58, "0");

What I don’t really know is the amount you want of zeros, so you should set a size limiter (your string’s limit)...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.