Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display absolute value angularjs

Tags:

I am getting a negative number from JSON object. I want to remove "-" form that negative number and only display the absolute value.

Received json:

{     "value": -2.34 } 

What I want to show:

The value is: 2.34

like image 497
Abhishek Kumar Avatar asked Dec 08 '15 04:12

Abhishek Kumar


2 Answers

You can use angular filter

js file

angular.module('myApp',[]).filter('makePositive', function() {     return function(num) { return Math.abs(num); } }); 

html file

{{ (-12) | makePositive }} {{ (2) | makePositive }} 

output

12 2

like image 140
fedorshishi Avatar answered Nov 12 '22 14:11

fedorshishi


You can use JavaScript supported built-in Math object for getting absolute value.

Math.abs(-2.34) 

The Math.abs() function returns the absolute value of a number

Reference

like image 34
Vivek Avatar answered Nov 12 '22 14:11

Vivek