How to remove numbers from a string?

Asked

Viewed 858 times

0

Hello, it should be something easy, but I’m cracking my head to solve.

I have a string, String(ValorTotal).replace("." , ""); //tira ponto This string returns me the following value: 23600000000000004 What I need is to remove these numbers 0000000000004 and leave only the 2360.

If anyone knows, help me!

  • What is the rule for separation? Do you want only the first 4 numbers? or all numbers before X zeros followed? or the whole part of a number? Explain it better so we can help.

  • 1

    What is the original value of ValorTotal?

  • @Sergio just want the first 4 numbers.

  • @Valdeirpsr would be this, var Total value = Valordis + Valordur, which would be the distance value plus the duration value.

1 answer

0


To capture only the first 4 digits, just use the function replace

var valor = String(ValorTotal).replace("." , "");
valor.substr(0, 4); // Captura do primeiro ao quarto dígito.
  • That helped me, you can help me with that, I have this code, Total value.toFixed(2), since the code comes like this 2.80, and other times like this, 6,000, so in this toFixed, I wanted to trade for when it’s 6,000 to trade the 2 for 3.

  • You can put a condition in this case. var toFixed = 2; if (valor == 6000) { toFixed = 3 }; ValorTotal.toFixed(toFixed);

  • I don’t think it’s gonna work.

  • If possible open a new question or create a chat room that we can better see the issue, otherwise comments can be removed later. This part of the code you can post right here in Stackoverflow or Codepen, for example.

  • Where to create a chat room?

  • https://chat.stackexchange.com/

  • enters this https://chat.stackexchange.com/rooms/70759/ajuda-ifelse

Show 3 more comments

Browser other questions tagged

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