If I have a total amount including VAT and want to separate the net price and the VAT value, how can this be done?
For example, if the final price is $80.60 with a VAT rate of 24%, what would be the net price and the VAT value? The correct answer should show the net price as $65.00 and the VAT value as $15.60.
I'm having an issue where typescript calculates the VAT value as 15.599999999999994 instead of rounding it to 15.60. Although there are other methods to calculate VAT, my main concern is why my code is generating this lengthy decimal.
Here is the code in question: component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-fpa',
templateUrl: './fpa.component.html',
styleUrls: ['./fpa.component.css']
})
export class FpaComponent implements OnInit {
public netPrice:number;
public fpaPercent:number=24;
public fpaValue:number=0;
public totalPrice:number;
public calc(calcType:string = ''){
this.netPrice = this.totalPrice / ((this.fpaPercent/100)+1);
this.fpaValue = this.totalPrice - this.netPrice;
}
}
component.html
<mat-form-field>
<input [(ngModel)]="netPrice" (keyup)="calc('byNetPrice');" matInput placeholder="Net Price">
</mat-form-field>
<mat-form-field>
<input [(ngModel)]="fpaPercent" (keyup)="calc();" matInput placeholder="% Vat">
</mat-form-field>
<mat-form-field>
<input [(ngModel)]="fpaValue" (keyup)="calc('byFpaValue');" matInput placeholder="VAT Value">
</mat-form-field>
<mat-form-field>
<input [(ngModel)]="totalPrice" (keyup)="calc('byFinalPrice');" matInput placeholder="Final Price" >
</mat-form-field>