Found 9150 Articles for Object Oriented Programming

Format integer values with java.text.DecimalFormat

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

147 Views

Here, we are using the DecimalFormat class in Java to format integer value, which is the java.text.DecimalFormat package.Let us take the following format: DecimalFormat("0.######E0") and use the format() method for this purpose −new DecimalFormat("0.#####E0").format(5089) new DecimalFormat("0.#####E0").format(608)Since, we have used DecimalFormat class in Java, therefore importing the following package in a must −import java.text.DecimalFormat;The following is an example −Example Live Demoimport java.text.DecimalFormat; public class Demo { public static void main(String[] argv) throws Exception { System.out.println(new DecimalFormat("0.#####E0").format(387979)); System.out.println(new DecimalFormat("0.#####E0").format(29797)); System.out.println(new DecimalFormat("0.#####E0").format(49788)); ... Read More

DecimalFormat("000000E0") in Java

Sai Subramanyam
Updated on 30-Jul-2019 22:30:24

165 Views

DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. Let us set DecimalFormat("000000E0") and use the format() method as well.new DecimalFormat("000000E0").format(199) new DecimalFormat("000000E0").format(29089)Since we have used DecimalFormat class in Java, therefore importing the following package is a must −import java.text.DecimalFormat;The following is the complete example −Example Live Demoimport java.text.DecimalFormat; public class Demo { public static void main(String[] argv) throws Exception { System.out.println(new DecimalFormat("000000E0").format(199)); System.out.println(new DecimalFormat("000000E0").format(29089)); System.out.println(new DecimalFormat("000000E0").format(398987)); System.out.println(new DecimalFormat("000000E0").format(498989)); ... Read More

Format double with new DecimalFormat("0.#####E0") in Java

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

180 Views

Firstly, set the double values −double d1 = 1.39876; double d2 = 2.39876; double d3 = 0.66920;Now, format the double values with new DecimalFormat("0.#####E0") −System.out.println(new DecimalFormat("0.#####E0").format(d1)); System.out.println(new DecimalFormat("0.#####E0").format(d2)); System.out.println(new DecimalFormat("0.#####E0").format(d3));The following is an example −Example Live Demoimport java.text.DecimalFormat; public class Demo { public static void main(String[] argv) throws Exception { double d1 = 1.39876; double d2 = 2.39876; double d3 = 0.66920; System.out.println(new DecimalFormat("0.#####E0").format(d1)); System.out.println(new DecimalFormat("0.#####E0").format(d2)); ... Read More

DecimalFormat("0.######E0") in Java

Sharon Christine
Updated on 30-Jul-2019 22:30:24

123 Views

DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. Let us set DecimalFormat("0.######E0") and use the format() method as well −new DecimalFormat("0.######E0").format(298757)Since, we have used DecimalFormat class in Java, therefore importing the following package in a must −import java.text.DecimalFormat;The following is the complete example −Example Live Demoimport java.text.DecimalFormat; public class Demo { public static void main(String[] argv) throws Exception { System.out.println(new DecimalFormat("0.######E0").format(298757)); System.out.println(new DecimalFormat("0.######E0").format(19988)); } }Output2.98757E5 1.9988E4

DecimalFormat(abc#) in Java

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

114 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 13-Nov-2024 17:17:20

147 Views

In this article, we will learn how to create BigDecimal values in Java using strings. The BigDecimal class provides a way to handle large or precise numbers, making it useful for high-precision arithmetic. By creating BigDecimal objects from strings, we make sure that the exact number is represented without any loss of precision. Problem Statement Given two large decimal values as strings, write a Java program to create BigDecimal objects and perform addition and multiplication on them.Input Value 1: "37578975.8768"Value 2: "62567878.9768"Output Value1 : 37578975.8768 Value 2 : 62567878.9768 Addition Operation = 100146854.8536 Multiplication Operation = 6265976294387200.48179648 Steps to ... 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

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

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

Advertisements