JavaScript Shorten long numbers to K M B

JavaScript convert numbers to K M B. 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) {
    num = num.toString().replace(/[^0-9.]/g, '');
    if (num < 1000) {
        return num;
    }
    let 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"}
      ];
    let index;
    for (index = si.length - 1; index > 0; index--) {
        if (num >= si[index].v) {
            break;
        }
    }
    return (num / si[index].v).toFixed(2).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, "$1") + si[index].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>