Found 9316 Articles for Object Oriented Programming

Math operations for BigDecimal in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

4K+ Views

Let us apply the following operations on BigDecimal using the in-built methods in Java −Addition: add() method Subtraction: subtract() method Multiplication: multiply() method Division: divide() methodLet us create three BigInteger objects −BigDecimal val1 = new BigDecimal("37578975587.876876989"); BigDecimal val2 = new BigDecimal("62567875598.976876569"); BigDecimal val3 = new BigDecimal("72567875598.376876569");Apply mathematical operations on them −val1 = val1.add(val2); System.out.println("Addition Operation = " + val1); val1 = val1.multiply(val2); System.out.println("Multiplication Operation = " + val1); val2 = val3.subtract(val2); System.out.println("Subtract Operation = " + val2);The following is an example −Example Live Demoimport java.math.BigDecimal; public class Demo { public static void main(String[] argv) throws Exception { ... Read More

Truncate BigDecimal value in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

2K+ Views

The truncate BigDecimal value in Java, try the following code.Here, we have taken two BigDecimla values and set the round mode for truncating the decimal values −Example Live Demoimport java.math.BigDecimal; public class Demo { public static void main(String[] argv) throws Exception { int decPlaces1 = 3; int decPlaces2 = 5; BigDecimal val1 = new BigDecimal("37578975587.876876989"); BigDecimal val2 = new BigDecimal("62567875598.976876569"); System.out.println("Value 1 : "+val1); ... Read More

Set Decimal Place of a Big Decimal Value in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

4K+ Views

We have the following BigDecimal value −BigDecimal val1 = new BigDecimal("37578975587.876876989");We have set the decimal place to be −int decPlaces1 = 3;Now, we will use the ROUND_DOWN field to set the rounding mode to round towards zero −// ROUND_DOWN val1 = val1.setScale(decPlaces1, BigDecimal.ROUND_DOWN); String str1 = val1.toString(); System.out.println("Result = "+str1);To set decimal place of a BigDecimal value in Java, try the following code −Example Live Demoimport java.math.BigDecimal; public class Demo { public static void main(String[] argv) throws Exception { int decPlaces1 = 3; int decPlaces2 = ... Read More

Negate a BigDecimal value in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

212 Views

Use the negate() method to negate a BigDecimal value in Java. The method returns a BigDecimal whose value is (-this), and whose scale is this.scale().The following is an example −Example Live Demoimport java.math.BigDecimal; public class Demo { public static void main(String[] argv) throws Exception { BigDecimal val1 = new BigDecimal("37578975587"); BigDecimal val2 = new BigDecimal("62567875598"); System.out.println("Value 1 : "+val1); System.out.println("Value 2 : "+val2); // division ... Read More

Java Program to divide one BigDecimal from another BigDecimal

Samual Sam
Updated on 30-Jul-2019 22:30:24

111 Views

Use the divide method to divide one BigDecimal to another in Java. The method returns a BigDecimal whose value is (this / divisor), and whose preferred scale is (this.scale() - divisor.scale()).If the exact quotient cannot be represented (because it has a non-terminating decimal expansion) an ArithmeticException is thrown.The following is an example −Example Live Demoimport java.math.BigDecimal; public class Demo { public static void main(String[] argv) throws Exception { BigDecimal val1 = new BigDecimal("37578975587"); BigDecimal val2 = new BigDecimal("62567875598"); System.out.println("Value 1 ... Read More

Subtract one BigDecimal from another BigDecimal in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

318 Views

Use the subtract method to subtract one BigDecimal to another in Java. TheBigDecimal.subtract(BigDecimal val) returns a BigDecimal whose value is (this - subtrahend), and whose scale is max(this.scale(), subtrahend.scale()). Here, “val” is the value to be subtracted from this BigDecimal.The following is an example −Example Live Demoimport java.math.BigDecimal; public class Demo { public static void main(String[] argv) throws Exception { BigDecimal val1 = new BigDecimal("375789755.345778656"); BigDecimal val2 = new BigDecimal("625678755.155778656"); System.out.println("Value 1 : "+val1); ... Read More

Multiply one BigDecimal to another BigDecimal in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

1K+ Views

Use the multiply() method to multiply one BigDecimal to another in Java. This method returns a BigDecimal whose value is (this × multiplicand), and whose scale is (this.scale() + multiplicand.scale()).The following is an example −Example Live Demoimport java.math.BigDecimal; public class Demo { public static void main(String[] argv) throws Exception { BigDecimal val1 = new BigDecimal("375789755.345778656"); BigDecimal val2 = new BigDecimal("525678755.155778656"); System.out.println("Value 1 = "+val1); System.out.println("Value 2 = "+val2); ... Read More

Create a BigDecimal via string in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

170 Views

Let us see how we can create BigDecimal values via string. Here, we have set string as a parameter to the BigDecimal constructor.BigDecimal val1 = new BigDecimal("375789755.345778656"); BigDecimal val2 = new BigDecimal("525678755.155778656");We can also perform mathematical operations on it −val2 = val2.subtract(val1);The following is an example −Example Live Demoimport java.math.BigDecimal; public class Demo { public static void main(String[] argv) throws Exception { BigDecimal val1 = new BigDecimal("375789755.345778656"); BigDecimal val2 = new BigDecimal("525678755.155778656"); System.out.println("Value 1 : "+val1); System.out.println("Value 2 : "+val2); val2 = val2.subtract(val1); System.out.println("Result (Subtraction) = "+val2); } }OutputValue 1 : 375789755.345778656 Value 2 : 525678755.155778656 Result (Subtraction) = 149888999.810000000

Create BigDecimal Values via a long in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

128 Views

Let us see how we can create BigDecimal values via a long. Here, we have set long values as a parameter to the BigDecimal constructor.BigDecimal val1 = BigDecimal.valueOf(289L); BigDecimal val2 = BigDecimal.valueOf(299L);We can also perform mathematical operations on it −val2 = val2.subtract(val1);The following is an example −Example Live Demoimport java.math.BigDecimal; public class Demo { public static void main(String[] argv) throws Exception { BigDecimal val1 = BigDecimal.valueOf(289L); BigDecimal val2 = BigDecimal.valueOf(299L); System.out.println("Value 1 : "+val1); ... Read More

Java Program to round a double passed to BigDecimal

Samual Sam
Updated on 30-Jul-2019 22:30:24

283 Views

The java.math.BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion.Firstly, let us pass a double to BigDecimal −BigDecimal val = new BigDecimal(9.19456);Now, we will round it −val = val.setScale(2, BigDecimal.ROUND_HALF_EVEN);Above, we have used the field ROUND_HALF_EVEN. It is a rounding mode to round towards the "nearest neighbor" unless both neighbours are equidistant, in which case, round towards the even neighboursThe following is an example −Example Live Demoimport java.math.BigDecimal; public class Demo { public static void main(String args[]) { BigDecimal val = new BigDecimal(9.19456); ... Read More

Advertisements