1

I know there are answers for plain JavaScript code for this question but they are not available in this case.

I have a table which must be populated with data. My code looks like this:

<tr ng-repeat="rows in $ctrl.matrix[0] track by $index">
    <td>{{$ctrl.labels[$index]}}</td>
    <td ng-repeat="label in $ctrl.xMatrix">{{$ctrl.matrix[$index][$parent.$index].toFixed(2)}}</td>
</tr>

As it can be seen, it is used toFixed(2) to remove all but two digits after dot.

I want also this change:

34.90 => 34.9
 0.00 => 0

As it says here, I parseFloat(n.toFixed(2));, so in my case it would be:

{{parseFloat($ctrl.matrix[$index][$parent.$index].toFixed(2))}} or

{{$ctrl.parseFloat(matrix[$index][$parent.$index].toFixed(2))}}

but in both cases I get no error and my table is empty.

Is there a way to remove these zeros inside {{}}?

Samurai Jack
  • 2,985
  • 8
  • 35
  • 58
  • Have a look here : https://stackoverflow.com/questions/3612744/remove-insignificant-trailing-zeros-from-a-number – Daniel Dec 07 '17 at 16:13
  • @Daniel that's what I've used. not working – Samurai Jack Dec 07 '17 at 16:15
  • Possible duplicate of [Remove insignificant trailing zeros from a number?](https://stackoverflow.com/questions/3612744/remove-insignificant-trailing-zeros-from-a-number) – Daniel Dec 07 '17 at 16:16
  • did you read my question? I already put a link to that question and explained why it does not work for me – Samurai Jack Dec 07 '17 at 16:16
  • Possible duplicate of [How to round in Javascript/Angular JS -- but remove insignificant digits](https://stackoverflow.com/questions/27850765/how-to-round-in-javascript-angular-js-but-remove-insignificant-digits) – Sanchit Patiyal Dec 07 '17 at 16:17
  • Did you read further in the comments. It has to be a number to work, cast as a number and then apply the solution. – Daniel Dec 07 '17 at 16:18

2 Answers2

2

Simple use number filter of Angular Js

{{$ctrl.matrix[$index][$parent.$index] | number}}

OR

Use $eval

{{$eval($ctrl.matrix[$index][$parent.$index])}}
Prafull
  • 591
  • 6
  • 16
0

Make sure you cast your number as a "number". Then cast as a string.

var numberstring = "1.2350000";

var number = Number(numberstring);
console.log(typeof number);

var zerodown = number.toString();
console.log(zerodown)

To change numbers inside {{}} is unlikely. This is a placeholder for variables that are dynamically applied. You should do that server side or after the value is parsed.

Daniel
  • 4,816
  • 3
  • 27
  • 31