1

I Have the following

var dividedResult = (893/ 200); var result = dividedResult.toFixed(decimalPlaces); 

The divided result is

4.465 

and the result is

4.5 

How do I stop the rounding in this case?

I want the result to be

4.4 

2 Answers 2

4

Try this 4.465 * 10 = 44.65 .. parseInt(44.65) = 44/10 = 4.4

result = parseInt(result * 10)/10; 

For any number of decimal places

result = parseInt(result * Math.pow(10,NumberOfDecimalPlaces))/(Math.pow(10,NumberOfDecimalPlaces)); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. The decimal places could be any number though
1

Expanding on Prasath's answer, if you wanted to distinguish between rounding up and rounding down to 1 decimal place you would do

Rounding down (4.4)

result = Math.floor(result * 10)/10; 

Rounding up (4.5)

result = Math.ceil(result * 10)/10; 

For your case, for any number of decimal places, use

result = Math.floor(result * Math.pow(10,decimalPlaces))/Math.pow(10,decimalPlaces); 

1 Comment

Thanks. The decimal places could be any number though

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.