Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

93
Visualizações
Reduce number with percent

I'm trying to implement a code check:

const transfer = async (  
  amount: string
): Promise<void> => {
  
  if (parseFloat(amount) <= parseFloat('0.01')) {
    amount = String((2 / 100) * amount);
  }
  .......
}

In general I want to make a check and reduce the amount with some very small percent. But I get error for this line * amount);:

TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.

Do you know how I can fix this issue?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You should be doing String((2 / 100) * parseFloat(amount));: you forgot to convert it to a number for arithmetic operations. Also, there is no need to use parseFloat(0.01) when you can use 0.01 directly. Your updated code would look like this:

const transfer = async (  
  amount: string
): Promise<void> => {
  
  if (parseFloat(amount) <= 0.01) {
    amount = String((2 / 100) * parseFloat(amount));
  }

  // More code here...
}

Also, if you want convert to string, a simpler way is to use template literals:

if (parseFloat(amount) <= 0.01) {
  amount = `2 / 100 * ${parseFloat(amount)}`;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

To resolve this, both string results have to be converted to number. This can be achieved by these 3 method:

  1. Unary Operator (+)
  2. parseFloat() //Already you used
  3. Multiply with number

1.Using Unary Operator (+) :

const transfer = async (  
  amount: string
): Promise<void> => {
  
  if (+amount <= 0.01) {
    amount = String(+(2 / 100) * +amount);
  }
   // More code here...
}

2.parseFloat() :

  const transfer = async (  
  amount: string
): Promise<void> => {
  
  if (parseFloat(amount) <= 0.01) {
    amount = String((2 / 100) * parseFloat(amount));
  }

  // More code here...
}

3.Multiply with number :

const transfer = async (  
      amount: string
    ): Promise<void> => {
      
      if ((amount*1) <= 0.01) {
        amount = String((2 / 100) * (amount*1));
      }
    
      // More code here...
    }

this error most off occurs by too much brackets.you need to try 1 method Unary Operator (+). it help to convert string into number and you don't need to wrap a string value into a brackets.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda