Java Round UP to 2 Decimals

TestRounding.java Source

import java.math.BigDecimal;

public class TestRounding {

 public TestRounding() {
 }

 public static void main(String[] args) {
  
  BigDecimal totalWeight = new BigDecimal("80.94999999999999");
  totalWeight = totalWeight.setScale(2, BigDecimal.ROUND_HALF_UP);
  System.out.println(totalWeight.toString());
  //Result = 80.95
  
  totalWeight = new BigDecimal("80.955000000001");
  totalWeight = totalWeight.setScale(2, BigDecimal.ROUND_HALF_UP);
  System.out.println(totalWeight.toString());
  //Result = 80.96

 }

}

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.