Found 34494 Articles for Programming

Java NumberFormat.getCurrencyInstance() method

karthikeya Boyini
Updated on 26-Jun-2020 09:10:36

3K+ Views

The getCurrencyInstance() method of the NumberFormat class returns the instance of the NumberFormat class. The java.text.NumberFormat class is used for formatting numbers and currencies as per a specific Locale. Number formats varies from country to countryHere, we have considered a locale.NumberFormat n = NumberFormat.getCurrencyInstance(Locale.FRANCE);Then, we have formatted a double value with the currency.double points = 1.78; System.out.println(n.format(points));The following is the final example.Example Live Demoimport java.text.NumberFormat; import java.util.Locale; public class MainClass {     public static void main(String[] args) {        // Currency of France is Euro        NumberFormat n = NumberFormat.getCurrencyInstance(Locale.FRANCE);        // points        double points = 1.78;        double totalPoints = points * 1000;        System.out.println(n.format(points));     ... Read More

Java NumberFormat.getInstance() method

Samual Sam
Updated on 26-Jun-2020 09:12:20

393 Views

The java.text.NumberFormat class is used for formatting numbers and currencies as per a specific Locale. Number formats varies from country to country.To get the instance of the NumberFormat class, call getInstance() method in Java.First, we will use the getInstance() method.NumberFormat n = NumberFormat.getInstance();Now, we will format a double.double val = 1.9898798; String formattedVal = n.format(val);The following is the final example.Example Live Demoimport java.text.NumberFormat; public class Demo {     public static void main(String args[]) {        NumberFormat n = NumberFormat.getInstance();        double val = 1.9898798;        System.out.println("Value: "+val);        String formattedVal = n.format(val);        System.out.println("Formatted Value: "+formattedVal);     } }OutputValue: 1.9898798 Formatted Value: 1.99Read More

Convert a String to a short number in Java

karthikeya Boyini
Updated on 26-Jun-2020 16:21:34

195 Views

Use the Short.parseShort() method in Java to convert a String to a short.Let’s say the following is our string.String str = “76”;Now, the parseShort() method will convert the string to short.byte val = Byte.parseByte(str);Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "76";        short val = Short.parseShort(str);        System.out.println(val);     } }Output76

Java Program to add leading zeros to a number

Samual Sam
Updated on 26-Jun-2020 09:15:38

14K+ Views

To add leading zeros to a number, you need to format the output. Let’s say we need to add 4 leading zeros to the following number with 3 digits.int val = 290;For adding 4 leading zeros above, we will use %07d i.e. 4+3 = 7. Here, 3, as shown above, is the number with 3 digits.String.format("%07d", val);The following is the final example.Example Live Demoimport java.util.Formatter; public class Demo {     public static void main(String args[]) {        int val = 290;        System.out.println("Integer: "+val);        String formattedStr = String.format("%07d", val);        System.out.println("With leading zeros = " + formattedStr);     } }OutputInteger: 290 With leading zeros = 0000290

Precision on a number format in Java

karthikeya Boyini
Updated on 26-Jun-2020 09:16:27

1K+ Views

You can include a precision specifier to the following format specifiers −%f %e %g %sOn floating point, the number of decimal places is known.Let’s say we declared a Formatter object −Formatter f1 = new Formatter();Now, we want 3 decimal places. For that, use 1.3f −f1.format("%1.3f", 29292929.98765432);The above will return the number with 3 decimal places −29292929.988The following is the final example −Example Live Demoimport java.util.Formatter; public class Demo {     public static void main(String args[]) {        Formatter f1,  f2,  f3;        f1 = new Formatter();        f1.format("%1.3f",  29292929.98765432);        System.out.println(f1);        f2 = new Formatter();        f2.format("%1.7f",  29292929.98765432);        System.out.println(f2);   ... Read More

Apply Ternary operator on double value in Java

Samual Sam
Updated on 26-Jun-2020 08:58:33

339 Views

The ternary operator is also known as the conditional operator. This operator consists of three operands and is used to evaluate Boolean expressions.Let’s say we have the following two double values.double val1 = 20.0; double val2 = 3.7;Now, let us use the ternary operator to check for the first value.System.out.println(val1 == 20 ? "Correct!" : "Incorrect!");If the above condition is correct i.e. val is equal to 20, therefore first statement “Correct” will be returned. If it isn’t equal, then the second statement will get evaluated.The following is the final example.Example Live Demoimport java.util.*; public class Demo {     public static void main(String args[]) {        double val1 = 20.0;   ... Read More

Compare Two Java double arrays

karthikeya Boyini
Updated on 26-Jun-2020 08:59:39

141 Views

To compare two Java double arrays, use the Arrays.equals() method. The following are our double arrays.double[] arr1 = new double[] { 1.3, 7.2, 4.2 }; double[] arr2 = new double[] { 1.3, 7.2, 4.2 }; double[] arr3 = new double[] { 2.8, 5.3, 9.8 }; double[] arr4 = new double[] { 2.9, 5.8, 7.8 };Now, let us compare the above arrays. Two of these arrays will be compared at once.Arrays.equals(arr1, arr2); Arrays.equals(arr1, arr3); Arrays.equals(arr1, arr4);The following is the final example to compare Java double arrays.Example Live Demoimport java.util.*; public class Demo {     public static void main(String args[]) {        double[] arr1 = new double[] { 1.3,  7.2,  4.2 };        double[] arr2 = new double[] { 1.3, ... Read More

Java Program to convert Double into String using toString() method of Double class

Samual Sam
Updated on 26-Jun-2020 09:00:12

79 Views

Let us first declare a Double −Double ob = new Double(99.12);Use the toString() method to convert Double into String.String res = ob.toString();The following is the complete example.Example Live Demopublic class Demo {    public static void main(String args[]) {       Double ob = new Double(99.12);       String res = ob.toString();       System.out.println(res);     } }Output99.12

Convert a String to a double type number in Java

karthikeya Boyini
Updated on 26-Jun-2020 09:00:45

216 Views

To convert a String to a double type number in Java, use the Double.parseDouble() method.Here is our string.String str = "699.7e130";Let us now convert the above string to double.double val = Double.parseDouble(str);Example Live Demopublic class Demo {     public static void main(String args[]) {        String str = "699.7e130";        double val = Double.parseDouble(str);        System.out.println(val);     } }Output6.997E132

Java Program to convert Double to numeric primitive data types

Samual Sam
Updated on 26-Jun-2020 09:03:00

1K+ Views

Let’s say we have Double object here.Double obj = new Double("39.45");Now, if we want to convert this Double to short primitive data type. For that, use the in-built shortValue() method.// converting to short primitive types short shortObj = obj.shortValue(); System.out.println(shortObj);In the same way convert Double to another numeric primitive data type int. For that, use the in-built intValue() method.// converting to int primitive types int intObj = obj.intValue(); System.out.println(intObj);The following is an example wherein we convert Double to numeric primitive types short, int, float, etc.Example Live Demopublic class Demo {    public static void main(String args[]) {       Double ... Read More

Advertisements