How to avoid scientific notation for large numbers in JavaScript?

后端 未结 22 2375
無奈伤痛
無奈伤痛 2020-11-22 00:31

JavaScript converts a large INT to scientific notation when the number becomes large. How can I prevent this from happening?

22条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 00:54

    function printInt(n) { return n.toPrecision(100).replace(/\..*/,""); }
    

    with some issues:

    • 0.9 is displayed as "0"
    • -0.9 is displayed as "-0"
    • 1e100 is displayed as "1"
    • works only for numbers up to ~1e99 => use other constant for greater numbers; or smaller for optimization.

提交回复
热议问题