0

I am trying to format one variable to always have 2 decimals, but I can't get how to do it for the following reason - it always either removes the 0 at the end or says:

Type 'string' is not assignable to type 'number'

The viewer_gain variable is type number, so that creates the issue, I can have a string with 2 decimals, but not number ...

This is what I am doing:

setCompensation() {
      // set these variables to string, to use toFixed(2)
      let value: string;
      let zero: string;
      if (isNaN(this.campaignModel.per_view_pay) || !this.compensateV) {

          // since toFixed is converting to string, i am trying to parseFloat later.
          zero = (0).toFixed(2); 

          // removes the 0 at the end here
          this.campaignModel.viewer_gain = parseFloat(zero);
      } else {

          // since toFixed is converting to string, i am trying to parseFloat later.
          value = (this.campaignModel.per_view_pay / 2).toFixed(2);

          // removes the 0 at the end here
          this.campaignModel.viewer_gain = parseFloat(value);
      }
    }

Anyone done this in another way?

chazsolo
  • 7,873
  • 1
  • 20
  • 44
Nemanja G
  • 1,760
  • 6
  • 29
  • 48

0 Answers0