0% found this document useful (0 votes)
18 views4 pages

Hands-On Assignment Command Line Argument

Uploaded by

Musiclover Huu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

Hands-On Assignment Command Line Argument

Uploaded by

Musiclover Huu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

String to int

 Integer.parseInt(String s)

String str = "123";


int num = Integer.parseInt(str);

 Integer.valueOf(String s)

String str = "123";


int num = Integer.valueOf(str); // Returns an Integer object

2. String to long

 Long.parseLong(String s)

String str = "123456789";


long num = Long.parseLong(str);

 Long.valueOf(String s)

String str = "123456789";


long num = Long.valueOf(str); // Returns a Long object

3. String to float
 Float.parseFloat(String s)

String str = "123.45";


float num = Float.parseFloat(str);

 Float.valueOf(String s)

String str = "123.45";


float num = Float.valueOf(str); // Returns a Float object

4. String to double
 Double.parseDouble(String s)

String str = "123.45";


double num = Double.parseDouble(str);

 Double.valueOf(String s)

String str = "123.45";


double num = Double.valueOf(str); // Returns a Double object

5. String to byte
 Byte.parseByte(String s)

String str = "127";


byte num = Byte.parseByte(str);

 Byte.valueOf(String s)

String str = "127";


byte num = Byte.valueOf(str); // Returns a Byte object

6. String to short
 Short.parseShort(String s)

String str = "12345";


short num = Short.parseShort(str);

 Short.valueOf(String s)

String str = "12345";


short num = Short.valueOf(str); // Returns a Short object

7. String to BigInteger

 new BigInteger(String s)
String str = "12345678901234567890";
BigInteger num = new BigInteger(str);

8. String to BigDecimal

 new BigDecimal(String s)

String str = "12345.6789";


BigDecimal num = new BigDecimal(str);

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.

int num = 123;


String str = String.valueOf(num); // recommended to use
2. Using Integer.toString(), Long.toString(), Float.toString(), Double.toString()

 These methods are specific to each primitive type and return the string representation of the respective
number.

int num = 123;


String str = Integer.toString(num);

long longNum = 123456789L;


String longStr = Long.toString(longNum);

float floatNum = 123.45f;


String floatStr = Float.toString(floatNum);

double doubleNum = 123.45;


String doubleStr = Double.toString(doubleNum);

3. Using String.format()

 This method allows for formatted output and can be used to convert numbers to strings with specific
formatting.

int num = 123;


String str = String.format("%d", num);

double doubleNum = 123.456;


String doubleStr = String.format("%.2f", doubleNum);

4. Using DecimalFormat
 This class provides a way to format numbers into strings with specific patterns.

import java.text.DecimalFormat;

double num = 12345.6789;


DecimalFormat df = new DecimalFormat("#.##");
String str = df.format(num);

5. Using StringBuilder or StringBuffer

 These classes provide methods to append numbers and then convert them to strings.

int num = 123;


StringBuilder sb = new StringBuilder();
sb.append(num);
String str = sb.toString();

6. Using toString() method on wrapper classes

 Wrapper classes like Integer, Long, Float, Double, Byte, and Short provide the toString() method.

Integer num = 123;


String str = num.toString();

Double doubleNum = 123.45;


String doubleStr = doubleNum.toString();

7. Using BigInteger.toString() and BigDecimal.toString()

 For BigInteger and BigDecimal, you can use their toString() methods.

import java.math.BigInteger;
import java.math.BigDecimal;

BigInteger bigInt = new BigInteger("12345678901234567890");


String bigIntStr = bigInt.toString();

BigDecimal bigDec = new BigDecimal("12345.6789");


String bigDecStr = bigDec.toString();

These methods provide various ways to convert numbers to strings, offering flexibility depending on the use case and
specific requirements.

You might also like