Found 9150 Articles for Object Oriented Programming

Java Program to convert Java Float to Numeric Primitive Data Types

Alshifa Hasnain
Updated on 30-Dec-2024 19:14:14

548 Views

The Float wrapper class provides methods to convert a Float object into various numeric primitive data types like short, int, float, and double in Java. This is particularly useful when handling float values and representing them in different numeric forms.This article will show two approaches to convert a Float object to numeric primitive types using the Float class methods. Using Float Wrapper Class Methods In this approach, we use predefined methods of the Float class, such as shortValue(), intValue(), floatValue(), and doubleValue(), to convert a Float object into various primitive types. These methods directly perform the required conversions. ... Read More

Java Program to convert string value to float using Float.valueOf()

Samual Sam
Updated on 26-Jun-2020 08:13:31

151 Views

The Float.valueOf() method is used in Java to convert string to float.The following are our string values.String str1 = "100.5"; String str2 = "200.5";To convert the string value to float, try the method valueOf()float val1 = (Float.valueOf(str1)).floatValue(); float val2 = (Float.valueOf(str2)).floatValue();The following is the complete example with output.Example Live Demopublic class Demo {    public static void main(String args[]) throws Exception {       String str1 = "100.5";       String str2 = "200.5";       float val1 = (Float.valueOf(str1)).floatValue();       float val2 = (Float.valueOf(str2)).floatValue();       System.out.println("Multiplication = " + (val1 * val2));    } }OutputMultiplication = 20150.25

Display the month number with SimpleDateFormat(“M”) in Java

karthikeya Boyini
Updated on 26-Jun-2020 08:16:50

3K+ Views

To display the month number, use the SimpleDateFormat(“M”)// displaying month number Format f = new SimpleDateFormat("M"); String strMonth = f.format(new Date()); System.out.println("Month Number = "+strMonth);Since, we have used the Format and SimpleDateFormat class above, therefore import the following packages. With that, we have also used the Date.import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date;Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo {    public static void main(String[] args) throws Exception {       // displaying current date and time       Calendar cal = Calendar.getInstance();       SimpleDateFormat simpleformat = new SimpleDateFormat("dd/MMMM/yyyy hh:mm:s");     ... Read More

Format date with SimpleDateFormat('MM/dd/yy') in Java

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

19K+ Views

Let us see how we can format date with SimpleDateFormat('MM/dd/yy')// displaying date Format f = new SimpleDateFormat("MM/dd/yy"); String strDate = f.format(new Date()); System.out.println("Current Date = "+strDate);Since, we have used the Format and SimpleDateFormat class above, therefore import the following packages. With that, we have also used the Date.import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date;Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo {    public static void main(String[] args) throws Exception {       // displaying current date and time       Calendar cal = Calendar.getInstance();       SimpleDateFormat simpleformat = new SimpleDateFormat("dd/MMMM/yyyy hh:mm:s");   ... Read More

Java Program to get full day name

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

5K+ Views

To display the full day name, use the SimpleDateFormat(“EEEE”) as shown below −// displaying full-day name f = new SimpleDateFormat("EEEE"); String str = f.format(new Date()); System.out.println("Full Day Name = "+str);Since, we have used the Format and SimpleDateFormat class above, therefore import the following packages. With that, we have also used the Date.import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date;Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo {    public static void main(String[] args) throws Exception {       // displaying current date and time       Calendar cal = Calendar.getInstance();       SimpleDateFormat simpleformat = ... Read More

Display the day in week using SimpleDateFormat('E') in Java

Samual Sam
Updated on 26-Jun-2020 08:20:51

663 Views

To display the day in week, use the SimpleDateFormat(“E”) as shown below −Format f = new SimpleDateFormat("E"); String strDayinWeek = f.format(new Date()); System.out.println("Day in week = "+strDayinWeek);Since, we have used the Format and SimpleDateFormat class above, therefore import the following packages. With that, we have also used the Date.import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date;Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo {    public static void main(String[] args) throws Exception {       // displaying current date and time       Calendar cal = Calendar.getInstance();       SimpleDateFormat simpleformat = new SimpleDateFormat("dd/MMMM/yyyy hh:mm:s"); ... Read More

Display two digits day number in Java

karthikeya Boyini
Updated on 26-Jun-2020 07:54:11

1K+ Views

To display two-digit day number, use the SimpleDateFormat('dd') as shown below −// displaying two-digit day number f = new SimpleDateFormat("dd"); String strDay = f.format(new Date()); System.out.println("Day Number = "+strDay);Since, we have used the Format and SimpleDateFormat class above, therefore import the following packages. With that, we have also used the Date.import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date;The following is an example.Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo {    public static void main(String[] args) throws Exception {       // displaying current date and time       Calendar cal = Calendar.getInstance();       ... Read More

Java program to check if a string is a valid number

Samual Sam
Updated on 05-Jul-2024 17:57:00

3K+ Views

In Java, verifying whether a string is a valid number is a common requirement in various applications. This process ensures that the string can be safely converted to a numerical type without causing runtime exceptions. To validate if a string can be converted to these types, we use the Integer.parseInt() and Float.parseFloat() methods, respectively. Problem Statement Given a string that may contain an integer or a float value, write a Java program to check whether it is valid number or not. Input "100" //string value Output 100 // ... Read More

Java Float Wrapper Class

karthikeya Boyini
Updated on 26-Jun-2020 08:02:31

2K+ Views

Float is a wrapper class provided to wrap float primitive value.Let us create a Float object with float primitive.// float primitive float myFloat = 24.22f; // Float object Float obj1 = new Float(myFloat);Let us now create a Float object from string.Float obj2 = new Float("39.87");The following is an example that displays both the ways discussed above with output.Example Live Demoimport java.lang.*; public class Demo {    public static void main(String args[]) {       float myFloat = 24.22f;       Float obj1 = new Float(myFloat);       System.out.println(obj1);       Float obj2 = new Float("39.87");       System.out.println(obj2);    } }Output24.22 39.87

Java Float isNaN() Method

Samual Sam
Updated on 26-Jun-2020 08:04:00

178 Views

The isNan() method returns true if the Float value is a Not-a-Number (NaN). Let’s say we have the following Float values.Float f1 = new Float(5.0/0.0); Float f2 = new Float(10.2/0.0); Float f3 = new Float(0.0/0.0);Check with isNaN() method.f1.isNaN (); f2.isNaN (); f3.isNaN ();The following is the complete example with output.Example Live Demoimport java.lang.*; public class Demo {    public static void main(String args[]) {       Float f1 = new Float(5.0/0.0);       Float f2 = new Float(10.2/0.0);       Float f3 = new Float(0.0/0.0);       System.out.println(f1.isNaN());       System.out.println(f2.isNaN());       System.out.println(f3.isNaN());   ... Read More

Advertisements