chop unused decimals with javascript

前端 未结 7 1062
鱼传尺愫
鱼传尺愫 2021-02-13 14:27

I\'ve got a currency input and need to return only significant digits. The input always has two decimal places, so:

4.00  ->  4
4.10  ->  4.1
4.01  ->           


        
7条回答
  •  囚心锁ツ
    2021-02-13 14:38

    I think this is what you want

        v = 33.404034
        v.toFixed(2) # v -> 33.40
        parseFloat(v.toFixed(2)) # 33.4
    

    and you have

        v = 33.00704034
        v.toFixed(2) # v -> 33.01
        parseFloat(v.toFixed(2)) # 33.01
    
        v = 33.00304034
        v.toFixed(2) # v -> 33.00
        parseFloat(v.toFixed(2)) # 33
    

提交回复
热议问题