user3310021
user3310021

Reputation: 123

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>

Upvotes: 4

Views: 5489

Answers (2)

Bhaskar
Bhaskar

Reputation: 1938

You can also use the js inbuilt Intl library

const formatter = new Intl.NumberFormat('en-IN', {
  style: 'currency',
  currency: 'INR',
  minimumFractionDigits: 0,
  maximumFractionDigits: 2,
  notation: 'compact',
  compactDisplay: 'long',
})

and use it like

const value = 200000
formatter.format(value) // => 2L

One can optionally change the locale en-IN to en-US to suit their style of notations.

It will also round off your value to the nearest notation to make it look cleaner.

Upvotes: 3

techfoobar
techfoobar

Reputation: 66663

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;
}

Upvotes: 11

Related Questions