Found 34494 Articles for Programming

DecimalFormat(abc#) in Java

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

82 Views

DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers.Let us set DecimalFormat(abc#) first −Format f = new DecimalFormat("'abc'#");Now, we will format a number and display the result in a string using the format() method −String res = f.format(-3968.7878);Since, we have used both Format and DecimalFormat class in Java, therefore importing the following packages in a must −import java.text.DecimalFormat; import java.text.Format;The following the complete example −Example Live Demoimport java.text.DecimalFormat; import java.text.Format; public class Demo { public static void main(String[] argv) throws Exception { Format f = new DecimalFormat("'abc'#"); ... Read More

Java Program to create Big Decimal Values via a string

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

65 Views

Let’s create BigDecimal value with a stringBigDecimal val1 = new BigDecimal("37578975.8768"); BigDecimal val2 = new BigDecimal("62567878.9768");You can also set the above as −String str1 = “37578975.8768”; BigDecimal val1 = new BigDecimal(str1); String str2 = “62567878.9768”; BigDecimal val2 = new BigDecimal(str2);Now, you can also perform operations like −val1 = val1.add(val2); System.out.println("Addition Operation = " + val1);The following is an example −Example Live Demoimport java.math.BigDecimal; public class Demo { public static void main(String[] argv) throws Exception { // creating BigDecimal values via a string BigDecimal val1 = new ... Read More

Working with BigDecimal values in Java

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

1K+ Views

The java.math.BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion.Two types of operations are provided for manipulating the scale of a BigDecimal −scaling/rounding operationsdecimal point motion operationsThe following are some of the constructors of the BigDecimal values −Sr.No.Constructor & Description1BigDecimal(BigInteger val)This constructor is used to translates a BigInteger into a BigDecimal.2BigDecimal(BigInteger unscaledVal, int scale)This constructor is used to translate a BigInteger unscaled value and an int scale into a BigDecimal.3BigDecimal(BigInteger unscaledVal, int scale, MathContext mc)This constructor is used to translate a BigInteger unscaled value and an int scale into a BigDecimal, with rounding according to ... Read More

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

315 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

Advertisements