How do you round to 1 decimal place in Javascript?

前端 未结 21 1970
难免孤独
难免孤独 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:01

    If your method does not work, plz post your code.

    However,you could accomplish the rounding off task as:

    var value = Math.round(234.567*100)/100
    

    Will give you 234.56

    Similarly

     var value = Math.round(234.567*10)/10
    

    Will give 234.5

    In this way you can use a variable in the place of the constant as used above.

提交回复
热议问题