Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In angular 2, how to display a number as two decimal rounded currency?

Examples:

  • 1.99 --> $1.99
  • 1.9 --> $1.90
  • 1 --> $1.00
  • 1.005 --> $1.01
  • 1.004 --> $1.00

I am using {{num | currency:'USD':true}} but it does not show trailing 0s.

like image 526
g.sui Avatar asked Jan 22 '16 05:01

g.sui


People also ask

How do you round a number to two decimal places typescript?

round() Function to Round a Number To2 Decimal Places in JavaScript. We take the number and add a very small number, Number. EPSILON , to ensure the number's accurate rounding. We then multiply by number with 100 before rounding to extract only the two digits after the decimal place.

How do you round to 2 decimal places in numbers?

Rounding a decimal number to two decimal places is the same as rounding it to the hundredths place, which is the second place to the right of the decimal point. For example, 2.83620364 can be round to two decimal places as 2.84, and 0.7035 can be round to two decimal places as 0.70.

How do you add two decimals in typescript?

length < 3) { // Set the number to two decimal places value = parseFloat(value). toFixed(2); } // Return updated or original number. return value; } and then this. percent = parseFloat(this.


1 Answers

Use this code. Here is a working example http://plnkr.co/edit/xnN1HnJtTel6WA24GttR?p=preview {{num | currency:'USD':true:'1.2-2'}}

Explanation :
number_expression | number[:digitInfo]

Finally we get a decimal number as text. Find the description.

number_expression: An angular expression that will give output a number.

number : A pipe keyword that is used with pipe operator.

digitInfo : It defines number format.

Now we will understand how to use digitInfo. The syntax for digitInfo is as follows.

{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}

Find the description.

minIntegerDigits : Minimum number of integer digits. Default is 1. (in our case 1)

minFractionDigits : Minimum number of fraction digits. Default is 0. (in our case 2)

maxFractionDigits : Maximum number of fraction digits. Default is 3. (in our case 2)

like image 133
Mubashir Avatar answered Sep 24 '22 06:09

Mubashir