Found 9326 Articles for Object Oriented Programming

Format floating point number in Java

Samual Sam
Updated on 26-Jun-2020 08:53:34

1K+ Views

Let’s say we have the following two values.double val1 = 20.932; double val2 = 11.67;Let us now format these floating-point numbers. Firstly, we are formatting euler’s number withMath.exp(). After that, we have also evaluated log. The %.3f you can see here is what we used for formatting the numbers.System.out.printf("exp(%.3f) = %.3f%n", val1, Math.exp(val1)); System.out.printf("log = %.3f%n", val1, Math.log(val1));The following is an example where we have also shown other ways to format float in Java.Example Live Demopublic class Demo {    public static void main(String args[]) {       double val1 = 20.932;       double val2 = 11.67;   ... Read More

Check two float arrays for equality in Java

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

113 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

119 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 26-Jun-2020 08:47:30

153 Views

Use the parseFloat() method in Java to convert a String to a float type.Let’s say we have the following string.String str = "111.8";Now, the Float.parseFloat() method will convert it to a float type number.float floatVal = Float.parseFloat(str);The following is the complete example with output.Example Live Demopublic class Demo {    public static void main(String args[]) {       String str = "111.8";       float floatVal = Float.parseFloat(str);       System.out.println("Float: "+floatVal);    } }OutputFloat: 111.8

Convert from String to float in Java

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

888 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

75 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

74 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

261 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

Display weekday names in Java

karthikeya Boyini
Updated on 26-Jun-2020 08:34:35

381 Views

To display weekday names in Java, use the getWeekdays() method.For that, firstly import the following package.import java.text.DateFormatSymbols;Now, create a string array and get all the weekday names using the getWeekdays() method.String[] weekDays = new DateFormatSymbols().getWeekdays();Example Live Demoimport java.text.DateFormatSymbols; public class Demo {    public static void main(String[] args) {       String[] weekDays = new DateFormatSymbols().getWeekdays();       System.out.println("Weekday names...");       for(String days: weekDays){       System.out.println(days);    } } }OutputWeekday names... Sunday Monday Tuesday Wednesday Thursday Friday Saturday

Advertisements