MRu
MRu

Reputation: 1395

How to round up or round down decimal places?

Let's say I want to suggest a bid for users in a reverse of forward manners.

For reverse:

For forward:

I've tried using math.floor() and math.ceil(), but it will only round it off to the nearest integer. Can you give me a solution or suggestion for this?

Upvotes: 1

Views: 135

Answers (1)

Dekel
Dekel

Reputation: 62606

You can use Math.floor(a*10)/10 and Math.ceil(a*10)/10 where a is the number you want to round.

console.log(Math.floor(1.23*10)/10)
console.log(Math.floor(2.35*10)/10)
console.log(Math.floor(3.59*10)/10)
console.log(Math.floor(4.99*10)/10)
console.log('')
console.log(Math.ceil(1.23*10)/10)
console.log(Math.ceil(2.35*10)/10)
console.log(Math.ceil(3.59*10)/10)
console.log(Math.ceil(4.99*10)/10)

If you need the final value as a string in the format X.YY (and not X.Y) you can use toFixed(2) (ie (Math.ceil(1.23*10)/10)).toFixed(2))

Upvotes: 5

Related Questions