Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format Numbers in K,Lacs,Crore

I'd like to format numbers into the numbers next to them with k, lacs, crores in jQuery:

1000 to 1K

1500 to 1.5K

100000 to 1Lac

150000 to 1.5Lac

1000000 to 10Lac

10000000 to 1cr and so on !

<!DOCTYPE html>
<html>
<head>
    <title>Number Differentiation</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(e){
              $('#numbr').bind('keypress', function (e) {
                                return (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57) && e.which != 46) ? false : true;
              });
            $("#submitBut").click(function(e){

                alert(numDifferentiation( $("#numbr").val()))
            })
            function numDifferentiation($val){
               return $val;
            }
        })
    </script>
</head>
<body>
<input id="numbr" type="text"/>
<button id="submitBut">Click</button>
</body>
</html>
like image 434
user3310021 Avatar asked Dec 19 '22 18:12

user3310021


1 Answers

This should do it:

function numDifferentiation(val) {
    if(val >= 10000000) val = (val/10000000).toFixed(2) + ' Cr';
    else if(val >= 100000) val = (val/100000).toFixed(2) + ' Lac';
    else if(val >= 1000) val = (val/1000).toFixed(2) + ' K';
    return val;
}
like image 116
techfoobar Avatar answered Jan 07 '23 23:01

techfoobar