Add trailing zeros to an integer without converting to string in JS?

后端 未结 2 1539
礼貌的吻别
礼貌的吻别 2021-01-28 05:50

I\'m looking to add decimals to the end of my integer. As an example:

15 => 15.00

The problem with methods like toFixed is that it will convert it into a string.

2条回答
  •  庸人自扰
    2021-01-28 06:28

    When working with numbers 15 and 15.00 are equal. It wouldn't make any sense to use memory to store those trailing or leading zeros. If that information is needed it is usually for displaying purposes. In that case a string is the right choice. In case you need that value again you can parse the string as a number.

提交回复
热议问题