how to extract decimal number from string using javascript

前端 未结 6 1986
春和景丽
春和景丽 2021-01-12 11:02

I want to extract decimal number 265.12 or 0.0 from alphanumeric string (say amount) having value $265.12+ or $265.12- or

6条回答
  •  清歌不尽
    2021-01-12 11:32

    A more elegant solution and also a method to avoid the 0.7700000004 javascript math do this:

    var num = 15.3354; Number(String(num).substr(String(num).indexOf('.')+1));

    The result will always be the exact number of decimals. Also a prototype for easier use

    Number.prototype.getDecimals = function () { return Number(String(this).substr(String(this).indexOf('.')+1)); }

    so now just 15.6655.getDecimals() ==> 6655

提交回复
热议问题