Home Remove trailing zeros from a number inside AngularJS interpolation
Reply: 1

Remove trailing zeros from a number inside AngularJS interpolation

user3654571
1#
user3654571 Published in 2017-12-07 16:11:47Z

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 {{}}?

Daniel
2#
Daniel Reply to 2017-12-07 16:21:56Z

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.40048 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO