We can remove the sign part of the data and display the absolute value by using abs function. Here is the syntax
Sample code is here
<script language='JavaScript' type='text/JavaScript'>
document.write(Math.abs(-5)); // output is 5
document.write(Math.abs(5.56)); // output is 5.56
document.write(Math.abs(-2.5)); // output is 2.5
document.write(Math.abs(-1.355)); // output is 1.355
abs() math function can also be used with other calculations
document.write(Math.abs(43-64)); // output is 21
We can integrate a demo script to calculate the absolute value of any integer.

Demo of Math.abs function

Here is the code
<script type="text/javascript">
function disp_data(){
var a1 = document.getElementById('t1').value;
document.getElementById("a2").innerHTML= a1;

<input type=text name=t1 id='t1' value=-12.78><input type=button value='Display rounded number' onClick='disp_data()';> 
<div id='a2'  style=" background-color: #c0f0c0; width:200" > Enter a number</div>

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
Visitors Rating
Your Rating


Post Comment This is for short comments only. Use the forum for more discussions.

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2018 All rights reserved worldwide Privacy Policy Disclaimer