Ep

Ep

How to Round Numbers in JavaScript

JavaScript round number to 2 decimal places

In this blog post I will demonstrate how to use javascript round function. Find the below javascipt code snippet :-

<script type="text/javascript">

// Function for round the number
    function roundNumber(num, dec) {
    var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec) + '';
    var r = result.indexOf('.');  
    return result;
    }

    </script>

math.round javascript 2 decimal places,javascript round to 1 decimal place,rounding off numbers in javascript,javascript restrict decimal places,javascript round function,round float value in javascript,javascript round off
rounding numbers calculator
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to Round the Number to Decimal in JavaScript Function - An Example</title>
   
    // Function for round the number
    function roundNumber(num, dec) {
    var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec) + '';
    var r = result.indexOf('.');  
    return result;
    }
   
    // Calling function
    function RoundOfNumber()
    {
        var num=document.getElementById("txtInputNo").value;
        var dec =document.getElementById("txtInputDec").value;
        alert(roundNumber(num,dec));    
    }
   
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Enter a Number:-
        <input type="text" id="txtInputNo" />
        Round to Decimal :-
        <input type="text" id="txtInputDec" />
        <br />
        <br />
        <a href="#" onclick="RoundOfNumber();">Click here to Round the Number</a>
    </div>
    </form>
</body>

</html>
How to Round Numbers in JavaScript Reviewed by Ravi Kumar on 4:50:00 PM Rating: 5

No comments:

All Rights Reserved by Etechpulse © 2012 - 2017
Powered By Blogger

Contact Form

Name

Email *

Message *

Powered by Blogger.