Found 9326 Articles for Object Oriented Programming

Set Decimal Place of a Big Decimal Value in Java

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

3K+ 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

206 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

110 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

309 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

127 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

277 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

BigInteger.isProbablePrime() method in Java

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

151 Views

TheBigInteger.isProbablePrime(int certainty) returns true if this BigInteger is probably prime, false if it's definitely composite. If certainty is ≤ 0, true is returned.Here, the “certainty” parameter is a measure of the uncertainty that the caller is willing to tolerate: if the call returns true the probability that this BigInteger is prime exceeds (1 - 1/2certainty). The execution time of this method is proportional to the value of this parameter.The following is an example −Example Live Demoimport java.math.BigInteger; public class Demo { public static void main(String[] argv) throws Exception { // create 3 ... Read More

Get byte array from BigInteger in Java

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

175 Views

First, set the BigInteger object with binary.BigInteger val = new BigInteger("100000000110001100000", 2);Now, use the toByteArray() method.byte[] byteArr = val.toByteArray();The following is an example −Example Live Demoimport java.math.BigInteger; public class Demo { public static void main(String[] argv) throws Exception { BigInteger val = new BigInteger("100000000110001100000", 2); byte[] byteArr = val.toByteArray(); for (int i = 0; i < byteArr.length; i++) { System.out.format("0x%02X", byteArr[i]); } } }Output0x10 0x0C 0x60

Advertisements