user696266
user696266

Reputation: 93

Rounding off in java

I have a list that contains many decimal values.

how should i go bt dng this?

Upvotes: 2

Views: 2536

Answers (4)

lschin
lschin

Reputation: 6811

Use MathUtils (Apache Commons Math)

MathUtils.round(double, scale, roundingMethod);

scale - The number of digits to the right of the decimal point. (+/-)
roundingMethod : BigDecimal.ROUND_DOWN, #.ROUND_CEILING, #.ROUND_HALF_UP, ...

Upvotes: 0

posdef
posdef

Reputation: 6562

If I understood you question correctly:

For the first point, you can use DecimalFormat class, see API

I'm not sure why you'd want to "round" 32.33 -> 32% as this is not rounding. If you are parsing user input data which could be given in this way, you can divide by 100 (giving 0.3233) and use DecimalFormat.

For the others you can use Math.round() as adviced below/above

Upvotes: 1

AbdullahC
AbdullahC

Reputation: 6730

Use Math.round(). This correctly rounds a number upwards if the fractional part is 0.5 or greater.

Upvotes: 4

Jigar Joshi
Jigar Joshi

Reputation: 240948

Math.round() and convert it to percent form.

I doubt about .032 case. rest are looking good.

Upvotes: 0

Related Questions