Use DecimalFormat , but specify the correct rounding mode. By default, ROUND_HALF_EVEN used, but ROUND_HALF_UP is common to many financial applications.
If you really want to find and use a rounded value in further calculations (which is unusual because it gives inaccurate results), you can use BigDecimal .
I assume that result1 and result2 are instances of String .
float value = Float.parseFloat(result1)*0.3F + Float.parseFloat(result2)*0.7F; BigDecimal d = new BigDecimal(String.valueOf(value)); BigDecimal rounded = d.setScale(1, BigDecimal.ROUND_HALF_EVEN);
The main advantage of using BigDecimal in contrast to multiplication, rounding and division, is that you can choose the rounding mode (half-up, half-even, etc.). A BigDecimal is much more expensive to manipulate than a primitive float , so that would be the best option for interactive financial applications than scientific modeling.
You can then use the rounded value as is, or convert it to a float . You can convert to String using the toString() method, but this is best done with a language-sensitive DecimalFormat object that will accept BigDecimal using the format() method.
source share