TL; dg; Option 1: return a string, do not convert to a number. Option 2: use the number tube.
toFixed returns a string.
You convert the result toFixed to a number
return +(this.getCost() * this.getQuantity()).toFixed(2); โ
When you convert "1.20" to a number, it โremovesโ the extra 0 returning 1.2 .
To show the result with two decimal places, you can return the result as a string so that 0 not cropped or used the channel.
You can use the number number
{{ 1.20 | number:'1.2-2' }}
Format: {minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}
minIntegerDigits - the minimum number of integer digits. The default is 1.minFractionDigits is the minimum number of digits after a fraction. The default is 0.maxFractionDigits - the maximum number of digits after the fraction. Default is 3.
With the pipe, you can save the code that returns the number, and it will only change how this value is formatted when you display it on the screen.
source share