Decimal Pipe in Angular 2 - Commas Only, No Decimal Places
Asked Answered
I

1

26

I currently use this pipe {{ product.productPrice | number:'.2-2' }}

And result is 1,000,000.00 but I want to remove the .00 How do you do that?

Impercipient answered 30/8, 2017 at 8:5 Comment(1)
@Vega: The edits were only on the title (from the OP, more descriptive) and from me (removed an inadequate tag), I don't think anything substantial from the question has been changed.Rabid
A
61

Use this :

  {{product.productPrice | number: '1.0-0'}}

1.0-0 means: at least one digit before decimal point, 0 digits after decimal point.

The general format is:

{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}

minIntegerDigits: The minimum number of integer digits before the decimal point. Default is 1.
minFractionDigits: The minimum number of digits after the decimal point. Default is 0. maxFractionDigits: The maximum number of digits after the decimal point. Default is 3.

https://angular.io/api/common/DecimalPipe

Ammerman answered 30/8, 2017 at 8:9 Comment(7)
what if I have 10 (100.10) after digit and don't want to remove it but want to remove if it is 100.00? do I need to handle it separately?Superorder
is it possible just get one digit before decimal point without rounding up? I mean -1.75 should be 1, not 2?Turbid
@StepUp, Angular decimal pipe rounds down and for the negative numbers that means up as a an absolute number. You can use just Math.trunc() before applying the pipe? Something like this: stackblitz.com/edit/angular-fbongyAmmerman
@Ammerman I am sorry, however I cannot see your code. Could you show, please, your code? I see just a page with code, however without Math.trunc().Turbid
I've seen Main.ts and polyfill.ts files, however there is no Math.trunc(). May be this file is not saved. Could you post here your code?Turbid
@Ammerman this {{ 3000 | number: '1.0-0' }} return 3.000, why?Norikonorina
@juanjinario, 3.000 is just formatted for thousands, it is not a decimal number. What are you looking to achieve?Ammerman

© 2022 - 2024 — McMap. All rights reserved.