
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9150 Articles for Object Oriented Programming

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

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

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

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

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

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

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

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

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

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