0

I am getting a value(for eg "75.8%") which is a string from am api and i want to use it as number so i can apply conditions

like this

<div 
   class="container" 
   [ngClass]="{
      'pos' : value > 0,
      'neg' : value < 0
   }"
   *ngIf="value"
>
   <span class="me-1">
      {{value}} % 
   </span>
                  
 </div>

Note:

Basically what i am doing is changing the background color to green (using class 'pos') if value is positive and to red if value is negative

I tried without converting the percent value as i dont know how to convert it

Rexxy
  • 1

1 Answers1

-1

Welcome to StackOverflow Rexxy

To solve your problem you can convert the value to float by using parseFloat(value). It will automatically ignore the %.

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat

Example:

const value = '74.4%';
console.log(parseFloat(value));