Showing posts with label BigDecimal. Show all posts
Showing posts with label BigDecimal. Show all posts

Saturday, 11 April 2020

Round BigDecimal to n decimal places

Using 'setScale' method of BigDecimal, you can specify the scale of the BigDecimal value to be returned.

For example, below snippet rounnd the decimal to 2 digits after '.'.
BigDecimal roundedValue = actualValue.setScale(2, RoundingMode.CEILING);

Find the below working application.

App.java
package com.sample.app;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class App {

 public static void main(String args[]) {
  BigDecimal[] bigDecimals = new BigDecimal[5];
  
  bigDecimals[0] = new BigDecimal("0.81934");
  bigDecimals[1] = new BigDecimal("0.5");
  bigDecimals[2] = new BigDecimal("0.564");
  bigDecimals[3] = new BigDecimal("0.54321");
  bigDecimals[4] = new BigDecimal("0.678951");
  
  System.out.println("Actual Value, Rounded Value");
  for(int i = 0; i < bigDecimals.length; i++) {
   BigDecimal actualValue = bigDecimals[i];
   BigDecimal roundedValue = actualValue.setScale(2, RoundingMode.CEILING);
   
   System.out.println(actualValue + "," + roundedValue);
  }
 }
}

Output

Actual Value, Rounded Value
0.81934,0.82
0.5,0.50
0.564,0.57
0.54321,0.55
0.678951,0.68



You may like

Friday, 13 March 2020

How to compare two BigDecimals?

Using compareTo function of BigDecimal you can compare, whether two decimals are equal, less than or greater than other.
private static void compare(BigDecimal decimal1, BigDecimal decimal2) {
 if (decimal1.compareTo(decimal2) == 1) {
  System.out.println(decimal1 + " is greater than " + decimal2);
 } else if (decimal1.compareTo(decimal2) == 0) {
  System.out.println(decimal1 + " is equal to " + decimal2);
 } else {
  System.out.println(decimal1 + " is less than " + decimal2);
 }
}

Find the below working application.

App.java
package com.sample.app;

import java.math.BigDecimal;

public class App {

 private static void compare(BigDecimal decimal1, BigDecimal decimal2) {
  if (decimal1.compareTo(decimal2) == 1) {
   System.out.println(decimal1 + " is greater than " + decimal2);
  } else if (decimal1.compareTo(decimal2) == 0) {
   System.out.println(decimal1 + " is equal to " + decimal2);
  } else {
   System.out.println(decimal1 + " is less than " + decimal2);
  }
 }

 public static void main(String[] args) {
  BigDecimal decimal1 = new BigDecimal("0.001");
  BigDecimal decimal2 = new BigDecimal("0.00");
  BigDecimal decimal3 = new BigDecimal("1");
  BigDecimal decimal4 = new BigDecimal("-1");

  compare(decimal1, BigDecimal.ZERO);
  compare(decimal1, decimal2);
  compare(decimal1, decimal3);
  compare(decimal1, decimal4);

 }

}

Output

0.001 is greater than 0
0.001 is greater than 0.00
0.001 is less than 1
0.001 is greater than -1


You may like

How to check BigDecimal is greater than 0?

Using ‘compareTo’ method of BigDecimal, you can check whether given number is > 0 or not.

BigDecimal decimal1 = new BigDecimal("0.001");

if (decimal1.compareTo(BigDecimal.ZERO) == 1) {
         System.out.println(decimal1 + " is greater than 0");
}

App.java
package com.sample.app;

import java.math.BigDecimal;

public class App {

 private static void compare(BigDecimal decimal1, BigDecimal decimal2) {
  if (decimal1.compareTo(decimal2) == 1) {
   System.out.println(decimal1 + " is greater than " + decimal2);
  } else if (decimal1.compareTo(decimal2) == 0) {
   System.out.println(decimal1 + " is equal to " + decimal2);
  } else {
   System.out.println(decimal1 + " is less than " + decimal2);
  }
 }

 public static void main(String[] args) {
  BigDecimal decimal1 = new BigDecimal("0.001");
  BigDecimal decimal2 = new BigDecimal("0.00");
  BigDecimal decimal3 = new BigDecimal("1");
  BigDecimal decimal4 = new BigDecimal("-1");

  compare(decimal1, BigDecimal.ZERO);
  compare(decimal1, decimal2);
  compare(decimal1, decimal3);
  compare(decimal1, decimal4);

 }

}

Output

0.001 is greater than 0
0.001 is greater than 0.00
0.001 is less than 1
0.001 is greater than -1


You may like