Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading