How do you round to 1 decimal place in Javascript?

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

    To complete the Best Answer:

    var round = function ( number, precision )
    {
        precision = precision || 0;
        return parseFloat( parseFloat( number ).toFixed( precision ) );
    }
    

    The input parameter number may "not" always be a number, in this case .toFixed does not exist.

提交回复
热议问题