JavaScript Shorten long numbers to K M B

Convert number to K M B javascript Code Example, Format a number thousand or more 1K

1000 to 1K

1000000 to 1M

1000000000 to 1B

Demo Number format
1.2K

Javascript Function Number Shorten

JavaScript
function intToString(num) {
    if (num < 1000) {
        return num;
    }
    var si = [
      {v: 1E3, s: "K"},
      {v: 1E6, s: "M"},
      {v: 1E9, s: "B"},
      {v: 1E12, s: "T"},
      {v: 1E15, s: "P"},
      {v: 1E18, s: "E"}
      ];
    var i;
    for (i = si.length - 1; i > 0; i--) {
        if (num >= si[i].v) {
            break;
        }
    }
    return (num / si[i].v).toFixed(2).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, "$1") + si[i].s;
}

Usage

HTML
<input type="number" value="1200" id="input-number">
<button type="button" id="convert">Convert</button>
<div id="result">1.2K</div>

<script>
document.getElementById("convert").addEventListener("click", function(){
    var number_val = document.getElementById("input-number").value;
    var result = intToString(number_val);
    document.getElementById("result").innerHTML = result;
});
</script>