2

I want to round a decimal number with 4 digit(e.g 4.3333) Is there any code in javascript?

I tried the following code

var dec=round(4.3234323,4);

4 Answers 4

6

There is .toFixed(n) method for number:

var dec = 4.3234323.toFixed(4);

And note 4.toFixed(4) will cause syntax error, you have to wrap it like (4).toFixed(4) or 4..toFixed(4),

it's always safe to used on a vairiable as var dec = num.toFixed(4);.

Sign up to request clarification or add additional context in comments.

Comments

2

Try the code

var num= 4.3234323;
var dec=(num).toFixed(4);

Comments

0

Try this:

var someValue = 4.3234323;
var decimalVal = someValue.toFixed(4);

Thanks.

Comments

0

For rounding the number, you can use -

var num = 4.3234323;
parseFloat(Math.ceil( num * 10 ) / 10).toFixed(4)

Working Example

OR you only want to take value at four decimal points then use - .toFixed(n)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.