cs notebook0
  • Introduction
  • Core Java
  • some notes
  • Data structure&algorithm
  • Garbage Collection
  • HashMap
  • Collection0
  • Collection1
  • Collection2
  • comparatorVScomparable
  • exception0
  • exception1
  • exception2
  • Enum in Java
  • JVM
  • Wrapper Classes in Java
  • String, int convert
  • HashSetVSTreeSetVSLinkedHashSet
  • Pair
Powered by GitBook
On this page
  • https://beginnersbook.com/2013/12/how-to-convert-string-to-double-in-java/
  • 1. String to Int
  • 1.1 e.g.
  • 1.2 e.g.
  • 1.3 e.g. NumberFormatException
  • 2. String to Double
  • 3. int to String

Was this helpful?

String, int convert

PreviousWrapper Classes in JavaNextHashSetVSTreeSetVSLinkedHashSet

Last updated 5 years ago

Was this helpful?

1. String to Int

1.1 e.g.

String number = "10";
int result = Integer.parseInt(number);
System.out.println(result);

1.2 e.g.

String number = "10";
Integer result = Integer.valueOf(number);
System.out.println(result);

1.3 e.g. NumberFormatException

If the string does not contain a parsable integer, aNumberFormatExceptionwill be thrown.

String number = "10A";
int result = Integer.parseInt(number);
System.out.println(result);

output: throw an Exception.

2. String to Double

e.g.

3. int to String

  1. String.valueOf()

e.g.

int var = 111;
String str = String.valueOf(var);
  1. Integer.toString()

e.g.

int ivar2 = 200;
String str2 = Integer.toString(ivar2);
https://beginnersbook.com/2013/12/how-to-convert-string-to-double-in-java/