Hands-On Assignment Command Line Argument
Hands-On Assignment Command Line Argument
Integer.parseInt(String s)
Integer.valueOf(String s)
2. String to long
Long.parseLong(String s)
Long.valueOf(String s)
3. String to float
Float.parseFloat(String s)
Float.valueOf(String s)
4. String to double
Double.parseDouble(String s)
Double.valueOf(String s)
5. String to byte
Byte.parseByte(String s)
Byte.valueOf(String s)
6. String to short
Short.parseShort(String s)
Short.valueOf(String s)
7. String to BigInteger
new BigInteger(String s)
String str = "12345678901234567890";
BigInteger num = new BigInteger(str);
8. String to BigDecimal
new BigDecimal(String s)
These methods provide robust ways to convert strings to various numeric types, handling different levels of precision
and range.
Number To String
In Java, converting a number to a String can be achieved using various methods provided by the standard library.
Here is a list of methods to convert different numerical types to a String:
1. Using String.valueOf()
This method can be used with any primitive data type and returns the string representation of the number.
These methods are specific to each primitive type and return the string representation of the respective
number.
3. Using String.format()
This method allows for formatted output and can be used to convert numbers to strings with specific
formatting.
4. Using DecimalFormat
This class provides a way to format numbers into strings with specific patterns.
import java.text.DecimalFormat;
These classes provide methods to append numbers and then convert them to strings.
Wrapper classes like Integer, Long, Float, Double, Byte, and Short provide the toString() method.
For BigInteger and BigDecimal, you can use their toString() methods.
import java.math.BigInteger;
import java.math.BigDecimal;
These methods provide various ways to convert numbers to strings, offering flexibility depending on the use case and
specific requirements.