How do you round to 1 decimal place in Javascript?

前端 未结 21 1467
难免孤独
难免孤独 2020-11-22 08:49

Can you round a number in javascript to 1 character after the decimal point (properly rounded)?

I tried the *10, round, /10 but it leaves two decimals at the end of

21条回答
  •  死守一世寂寞
    2020-11-22 09:08

    lodash has a round method:

    _.round(4.006);
    // => 4
    
    _.round(4.006, 2);
    // => 4.01
    
    _.round(4060, -2);
    // => 4100
    

    Docs.

    Source.

提交回复
热议问题