Found 33676 Articles for Programming

Java Program to copy an array from the specified source array

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

179 Views

Use arraycopy() method in Java to copy an array from the specified source array.Here, we have two arrays −int arr1[] = { 10, 20, 30, 40}; int arr2[] = { 3, 7, 20, 30};Now, we will use the arraycopy() method to copy the first two elements of the 1st array to the 2nd array −System.arraycopy(arr1, 0, arr2, 2, 2);The following is an example −Example Live Demoimport java.lang.*; public class Demo { public static void main(String[] args) { int arr1[] = { 10, 20, 30, 40}; int arr2[] = { 3, 7, 20, 30}; System.arraycopy(arr1, 0, arr2, 2, 2); System.out.print("New Array = "); System.out.print(arr2[0] + " "); System.out.print(arr2[1] + " "); System.out.print(arr2[2] + " "); System.out.print(arr2[3] + " "); } }OutputNew Array = 3 7 10 20

Java Program to round number to fewer decimals

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

156 Views

Let’s say we need to round number to 3 decimal places, therefore use the following format −DecimalFormat decFormat = new DecimalFormat("0.000");Now, format the number −decFormat.format(37878.8989)Since we have used the DecimalFloat class above, therefore do import the following package −import java.text.DecimalFormat;The following is an example that rounds a number to 3 decimal places −Example Live Demoimport java.text.DecimalFormat; public class Demo {    public static void main(String[] args) {       DecimalFormat decFormat = new DecimalFormat("0.000"); System.out.println("Result = " + decFormat.format(37878.8989)); } }OutputResult = 37878.899Let us see another example that rounds a number ... Read More

DecimalFormat("###E0") in Java

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

135 Views

DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. Let us set DecimalFormat("###E0") and use the format() method as well.DecimalFormat decFormat = new DecimalFormat("##E0"); System.out.println(decFormat.format(-267.9965)); System.out.println(decFormat.format(8.19)); System.out.println(decFormat.format(9897.88));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 { DecimalFormat decFormat = new DecimalFormat("###E0"); System.out.println(decFormat.format(-267.9965)); System.out.println(decFormat.format(8.19)); ... Read More

DecimalFormat("##E0") in Java

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

95 Views

DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. Let us set DecimalFormat("##E0") and use the format() method as well.DecimalFormat decFormat = new DecimalFormat("##E0"); System.out.println(decFormat.format(-189.8787)); System.out.println(decFormat.format(8.19)); System.out.println(decFormat.format(9897.88));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 { DecimalFormat decFormat = new DecimalFormat("##E0"); System.out.println(decFormat.format(-189.8787)); System.out.println(decFormat.format(8.19)); ... Read More

DecimalFormat("00.00E0") in Java

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

319 Views

DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. Let us set DecimalFormat("00.00E0") and use the format() method as well.DecimalFormat decFormat = new DecimalFormat("00.00E0"); System.out.println(decFormat.format(-289.8787)); System.out.println(decFormat.format(8.19));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 { DecimalFormat decFormat = new DecimalFormat("00.00E0"); System.out.println(decFormat.format(-289.8787)); System.out.println(decFormat.format(8.19)); System.out.println(decFormat.format(9897.88)); ... Read More

Display a currency value in Java

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

221 Views

To display a currency in Java, use the following DecimalFormat −DecimalFormat decFormat = new DecimalFormat("\u00a4#, ##0.00");Since, we have used the DecimalFormat class, therefore do not forget to import the following package −import java.text.DecimalFormat;Now, let us learn how to display percentage −decFormat.format(877.80) decFormat.format(8.19) decFormat.format(9897.88)The above will be displayed as −$877.80 $8.19 $9, 897.88The following is the complete example −Example Live Demoimport java.text.DecimalFormat; public class Demo { public static void main(String[] argv) throws Exception { // for currency DecimalFormat decFormat = new DecimalFormat("\u00a4#, ##0.00"); ... Read More

Display a percentage in Java

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

2K+ Views

To display a percentage in Java, use the following DecimalFormat.DecimalFormat decFormat = new DecimalFormat("#%");Since, we have used the DecimalFormat class, therefore do not forget to import the following package −import java.text.DecimalFormat;Now, let us learn how to display percentage −decFormat.format(0.80) decFormat.format(-0.19) decFormat.format(1.88)The above will be displayed as −80% -19% 188%The following is the complete example −Example Live Demoimport java.text.DecimalFormat; public class Demo { public static void main(String[] argv) throws Exception { DecimalFormat decFormat = new DecimalFormat("#%"); System.out.println(decFormat.format(0.80)); System.out.println(decFormat.format(-0.19)); ... Read More

Display numbers with leading zeroes in Java

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

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

153 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

168 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

Advertisements