Found 34494 Articles for Programming

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

280 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

155 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

182 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

Math Operations on BigInteger in Java

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

2K+ Views

Let us apply the following operations on BigInteger using the in-built methods in Java.Addition: add() method Subtraction: subtract() method Multiplication: multiply() method Division: divide() methodLet us create three BigInteger objects.BigInteger one = new BigInteger("98765432123456789"); BigInteger two = new BigInteger("94353687526754387"); BigInteger three = new BigInteger("96489687526737667");Apply mathematical operations on them.one = one.add(two); System.out.println("Addition Operation = " + one); one = one.multiply(two); System.out.println("Multiplication Operation = " + one);The following is an example −Example Live Demoimport java.math.BigInteger; public class Main { public static void main(String[] args) { BigInteger one = new BigInteger("98765432123456789"); ... Read More

Convert BigInteger into another radix number in Java

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

874 Views

First, create a BigInteger.BigInteger val = new BigInteger("198");Let us convert it to Binary, with radix as 2.val.toString(2);Convert it to Octal, with radix as 8.val.toString(8);Convert it to HexaDecimal, with radix as 16.val.toString(16);The following is an example −Example Live Demoimport java.math.BigInteger; public class Main { public static void main(String[] args) { BigInteger val = new BigInteger("198"); System.out.println("Value: " + val); // binary System.out.println("Converted to Binary: " + val.toString(2)); // octal ... Read More

Parse and format to arbitrary radix <= Character.MAX_RADIX in Java

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

186 Views

Let us set a radix here as.// radix int r = 32;Include the radix here as a BigInteger constructor.BigInteger one = new BigInteger("vv", r);Now get its string representation −String strResult = one.toString(radix);The following is an example −Example Live Demoimport java.math.*; public class Demo { public static void main(String[] args) { // radix int r = 32; BigInteger one = new BigInteger("vv", r); String strResult = one.toString(r); System.out.println(strResult); } }Outputvv

Parsing and Formatting a Byte Array into Decimal in Java

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

2K+ Views

Set a BigInteger object.BigInteger one;Now, create a ByteArray −byte byteArr[] = new byte[] { 0x1, 0x00, 0x00 }; one = new BigInteger(byteArr);For Decimal, we have used toString() method without any parameter value.String strResult = one.toString();The following is an example −Example Live Demoimport java.math.*; public class Demo { public static void main(String[] args) { BigInteger one; // ByteArray byte byteArr[] = new byte[] { 0x1, 0x00, 0x00 }; one = new BigInteger(byteArr); // Decimal String strResult = one.toString(); System.out.println("ByteArray to Decimal = "+strResult); } }OutputByteArray to Decimal = 65536

Parsing and Formatting a Byte Array into Octal in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:43:13

458 Views

Set a BigInteger object.BigInteger one;Now, create a ByteArray.byte byteArr[] = new byte[] { 0x1, 0x00, 0x00 }; one = new BigInteger(byteArr);For Octal, we have used 8 as the toString() method parameter.String strResult = one.toString(8);The following is an example −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one;       // ByteArray       byte byteArr[] = new byte[] { 0x1, 0x00, 0x00 };       one = new BigInteger(byteArr);       // Octal       String strResult = one.toString(8);       System.out.println("ByteArray to Octal = "+strResult);    } }OutputByteArray to Octal = 200000

Advertisements