Found 7442 Articles for Java

Convert double primitive type to a Double object in Java

karthikeya Boyini
Updated on 26-Jun-2020 08:52:45

8K+ Views

To convert double primitive type to a Double object, you need to use Double constructor.Let’s say the following is our double primitive.// double primitive double val = 23.78;To convert it to a Double object, use Double constructor.// Double object Double ob = new Double(val);Example Live Demopublic class Demo {    public static void main(String args[]) {       // double primitive       double val = 23.78;       // Double object       Double ob = new Double(val);       System.out.println(ob);    } }Output23.78

Format floating point number in Java

Revathi Satya Kondra
Updated on 13-Dec-2024 13:38:06

4K+ Views

What is Floating Point Number? Floating point number formatting is the process of controlling how floating point numbers (numbers with decimal points) are displayed(printed in a program). For example, if we have a decimal number of 123.45678, the formatted number can be 123.457, 123.46, and 123.5 instead of that number. This makes the numbers easier to read and more consistent. The Floating point is just a number with decimal points. For example 1.2, 2.45 and 123.4 are the examples of the point numbers. It helps ensure the number of decimal places, making numerical data easier to read and maintaining a ... Read More

Check two float arrays for equality in Java

karthikeya Boyini
Updated on 26-Jun-2020 08:45:58

181 Views

To check two float arrays for equality, use the Arrays.equals() method.In our example, we have the following two float arrays.float[] floatVal1 = new float[] { 3.2f, 5.5f, 5.3f }; float[] floatVal2 = new float[] { 3.2f, 5.5f, 5.3f };Let us now compare them for equality.if (Arrays.equals(floatVal1, floatVal2)) { System.out.println("Both are equal!"); }The following is an example wherein we compare arrays and with that also use == to check for other conditions.Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String args[]) {       float[] floatVal1 = new float[] { 3.2f, 5.5f, 5.3f };       ... Read More

Java Program to compare Two Java float Arrays

Samual Sam
Updated on 26-Jun-2020 08:46:48

202 Views

To compare Java float arrays, use the Arrays.equals() method. The return value is a boolean. Let’s say we have the following float arrays −float[] floatVal1 = new float[] { 3.2f, 5.5f, 5.3f }; float[] floatVal2 = new float[] { 8.3f, 8.8f, 9.2f }; float[] floatVal3 = new float[] { 6.2f, 6.9f, 9.9f }; float[] floatVal4 = new float[] { 3.2f, 5.5f, 5.3f };To compare them, the Arrays.equals() method is to be used −Arrays.equals(floatVal1, floatVal2); Arrays.equals(floatVal2, floatVal3); Arrays.equals(floatVal3, floatVal4);The following is the complete example wherein we compare all the arrays −Example Live Demoimport java.util.Arrays; public class Demo {    public static void ... Read More

Java Program to convert a String to a float type Number

karthikeya Boyini
Updated on 23-Jan-2025 22:54:22

224 Views

In this article, we will learn how to convert a string to a float-type number in Java. We will use the Float.parseFloat() method to perform this conversion. Float.parseFloat() method The Float.parseFloat() method belongs to the Float class in Java and is used to convert a string representation of a number into a primitive float. If the string does not contain a valid float representation, it throws a NumberFormatException. NumberFormatException: Thrown to show that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format. ... Read More

Convert from String to float in Java

Samual Sam
Updated on 26-Jun-2020 08:48:08

1K+ Views

To convert String to float, use the valueOf() method.Let’s say we have the following string value.String str = "0.8";Converting the string to float.Float floatVal = Float.valueOf(str).floatValue();The following is the complete example.Example Live Demopublic class Demo {    public static void main(String args[]) {       String str = "0.8";       Float floatVal = Float.valueOf(str).floatValue();       System.out.println("Float: "+floatVal);    } }OutputFloat: 0.8

Convert from float to String in Java

karthikeya Boyini
Updated on 26-Jun-2020 08:21:49

9K+ Views

To convert float to string, use the toString() method. It represents a value in a string.Let’s say the following is our float.float f = 0.9F;Converting the float value to string.String s = Float.toString(f);Let us see the complete example to convert float to String in Java with output.Example Live Demopublic class Demo {    public static void main(String args[]) {       float f = 0.9F;       String s = Float.toString(f);       System.out.println(s);    } }Output0.9

Java Program to return a Date set to the first possible millisecond of the day after midnight

Samual Sam
Updated on 26-Jun-2020 08:24:18

130 Views

Let us first set the calendar object.Calendar calendar = Calendar.getInstance();Use the getMinimum() method in Java to return the minimum value for the given calendar field. We will use it to set the minute, hours second and milliseconds.For hour and minute.calendar.set(Calendar.HOUR_OF_DAY, calendar.getMinimum(Calendar.HOUR_OF_DAY)); calendar.set(Calendar.MINUTE, calendar.getMinimum(Calendar.MINUTE));For second and milliseconds.// second calendar.set(Calendar.SECOND, calendar.getMinimum(Calendar.SECOND)); // millisecond calendar.set(Calendar.MILLISECOND, calendar.getMinimum(Calendar.MILLISECOND));The following is an example that returns a Date set to the first possible millisecond of the day after midnight.Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo {    public static void main(String[] argv) throws Exception {       Calendar calendar = Calendar.getInstance();       // ... Read More

Java Program to return a Date set to the last possible millisecond of the day before midnight

karthikeya Boyini
Updated on 26-Jun-2020 08:23:20

153 Views

Let us first set the calendar object.Calendar calendar = Calendar.getInstance();Use the getMaximum() method in Java to returns the maximum value for the given calendar field. We will use it to set the minute, hours second and milliseconds.For hour and minute.calendar.set(Calendar.HOUR_OF_DAY, calendar.getMaximum(Calendar.HOUR_OF_DAY)); calendar.set(Calendar.MINUTE, calendar.getMaximum(Calendar.MINUTE));For second and milliseconds.// second calendar.set(Calendar.SECOND, calendar.getMaximum(Calendar.SECOND)); // millisecond calendar.set(Calendar.MILLISECOND, calendar.getMaximum(Calendar.MILLISECOND));The following is an example that returns a Date set to the last possible millisecond of the day before midnight.Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo {    public static void main(String[] argv) throws Exception {       Calendar calendar = Calendar.getInstance();       // ... Read More

Format and Parse Date in Java

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

318 Views

To format a date in Java, firstly import the following package.import java.text.DateFormat;Now, create DateFormat object.DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT); DateFormat longFormat = DateFormat.getDateInstance(DateFormat.LONG);Use the format() method to format the above dates.System.out.println(shortFormat.format(new Date())); System.out.println(longFormat.format(new Date()));To parse the dates, use the parse() method.Example Live Demoimport java.text.DateFormat; import java.text.ParseException; import java.util.Date; public class Demo {    public static void main(String[] args) throws Exception {       // format date       DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT);       DateFormat longFormat = DateFormat.getDateInstance(DateFormat.LONG);       System.out.println("Format Date...");       System.out.println(shortFormat.format(new Date()));       System.out.println(longFormat.format(new Date()));       // parse date ... Read More

Advertisements