Found 9326 Articles for Object Oriented Programming

Display numbers with leading zeroes in Java

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

2K+ Views

To display numbers with leading zeros in Java, useDecimalFormat("000000000000").Let us first set the format with DecimalFormat class −DecimalFormat decFormat = new DecimalFormat("000000000000");Now, let us display some numbers with leading zeros −String res = decFormat.format(298989); System.out.println(res); res = decFormat.format(565); System.out.println(res);The following is an example −Example Live Demoimport java.text.DecimalFormat; public class Demo { public static void main(String args[]) { DecimalFormat decFormat = new DecimalFormat("000000000000"); String res = decFormat.format(298989); System.out.println(res); res = decFormat.format(565); ... Read More

Format integer values with java.text.DecimalFormat

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

77 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

97 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

131 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

74 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

81 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

64 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

Advertisements