0

I'm working in an existing project in angularjs and I'm newbie.

I have the following code:

<td ng-if="obj.value != ''">{{obj.value}}</td>

I need to check whether obj.value has decimal part or not. I yes I want to limit the decimal part to 1 digit.

I tried <td ng-if="obj.value != ''">{{obj.value|number:1}}</td> but converts also integer values to decimals.

Any ideas?

zinon
  • 4,427
  • 14
  • 70
  • 112
  • So if it is 10, you just want 10 and not 10.0 ? – epascarello Sep 14 '20 at 13:51
  • I don't know Angular syntax, but have you tried something like `{{obj.value|number:1}}`? - [Reference to check if a number is float in Javascript](https://stackoverflow.com/questions/2304052/check-if-a-number-has-a-decimal-place-is-a-whole-number) – xKobalt Sep 14 '20 at 13:53

1 Answers1

1

You can simply do it like this:

<td ng-if="obj.value != ''">{{obj.value | number: obj.value % 1 === 0 ? 0 : 1}}</td>

You find a more detailed explanation about the number pipe here in this documentation and regarding checking integer there are multiple answers but you can refer this question for them.

Black Mamba
  • 13,632
  • 6
  • 82
  • 105