Found 9150 Articles for Object Oriented Programming

Java NumberFormat.getPercentageInstance() Method

Samual Sam
Updated on 28-Jan-2025 14:58:02

611 Views

In this article, we will learn the NumberFormat.getPercentageInstance() method. Java provides powerful tools for formatting numbers, currencies, and percentages through the java.text.NumberFormatclass. One of the most useful methods in this class is the getPercentageInstance() method. NumberFormat.getPercentageInstance() method The NumberFormat.getPercentageInstance() method is a static method that returns a NumberFormat instance configured to format numbers as percentages. This method can be used with or without a Locale parameter.  Syntax − public static NumberFormat getPercentageInstance()public static NumberFormat getPercentageInstance(Locale locale) Without Locale: If no locale is specified, the method uses the default locale of the JVM. ... Read More

Java NumberFormat.getCurrencyInstance() method

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

4K+ 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 04-Nov-2024 18:41:47

558 Views

In this article, we will learn how to format numbers in Java using the NumberFormat class from the java.text package. The NumberFormat class provides methods for formatting numbers based on Locale settings, which is useful for creating internationalized Java applications. Using the NumberFormat.getInstance() method, we can format double, int, or float values in a Java program.  Problem StatementGiven a decimal value, we need to format the number using Java's NumberFormat class to round it according to the default Locale settings.Input double val = 1.9898798; Output Formatted Value: 1.99 ... Read More

Convert a String to a short number in Java

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

298 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

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

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

511 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

244 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

136 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

294 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

Advertisements