What is the best method to convert floating point to an integer in JavaScript?

后端 未结 12 2134
梦如初夏
梦如初夏 2020-12-08 05:00

There are several different methods for converting floating point numbers to Integers in JavaScript. My question is what method gives the best performance, is most compatibl

12条回答
  •  天涯浪人
    2020-12-08 05:14

    The answer has already been given but just to be clear.

    Use the Math library for this. round, ceil or floor functions.

    parseInt is for converting a string to an int which is not what is needed here

    toFixed is for converting a float to a string also not what is needed here

    Since the Math functions will not be doing any conversions to or from a string it will be faster than any of the other choices which are wrong anyway.

提交回复
热议问题