To round x postion of decimal point, use BigDecimal.setScale().

double requestPerSecond = httpInfo.getRequestPerSencond();
BigDecimal reqPerSec =
     new BigDecimal(requestPerSecond).setScale(2,java.math.BigDecimal.ROUND_HALF_UP);

setScale

public BigDecimal setScale(int scale,
                           int roundingMode)
Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value. If the scale is reduced by the operation, the unscaled value must be divided (rather than multiplied), and the value may be changed; in this case, the specified rounding mode is applied to the division.

Note that since BigDecimal objects are immutable, calls of this method do not result in the original object being modified, contrary to the usual convention of having methods named setX mutate field X. Instead, setScale returns an object with the proper scale; the returned object may or may not be newly allocated.

Parameters:
scale - scale of the BigDecimal value to be returned.
roundingMode - The rounding mode to apply.
Returns:
a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value.
Throws:
ArithmeticException - scale is negative, or roundingMode==ROUND_UNNECESSARY and the specified scaling operation would require rounding.
IllegalArgumentException - roundingMode does not represent a valid rounding mode.

'java core' 카테고리의 다른 글

JMX reference  (0) 2009.04.29
Effective Java Reload  (0) 2009.04.29
How to get cpu usage in java.  (0) 2009.04.29
How to get hostname in http url in Java  (0) 2009.04.28
G1 garbage collection  (0) 2009.04.28
Posted by '김용환'
,