- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 4338 Articles for Java 8
394 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
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
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
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
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
142 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
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
217 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
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
328 Views
The java.lang.Math class contains methods for performing basic numeric operations such as the trigonometry, logarithm, etc.The following are some of the methods.Sr.NoMethods & Description1static double abs(double a)This method returns the absolute value of a double value.2static float abs(float a)This method returns the absolute value of a float value.3static int abs(int a)This method returns the absolute value of an int value.4static long abs(long a)This method returns the absolute value of a long value.5static double acos(double a)This method returns the arc cosine of a value; the returned angle is in the range 0.0 through pi.6static double asin(double a)This method returns the arc ... Read More