0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Java】数値と文字列の変換方法 (valueOf)

Posted at

数値を文字列に変換

valueOfメソッドで数値を文字列に変換できます。

public class Sample {
	public static void main(String[] args) {

		int num1 = 2021; // int型
		String a = String.valueOf(num1);//String型に変換
		System.out.println(a);//2021

		long num2 = 2021; // long型
		String b = String.valueOf(num2);//String型に変換
		System.out.println(b);//2021

		double num3 = 2021.21; // double型
		String c = String.valueOf(num3);//String型に変換
		System.out.println(c);//2021.21
	}
}

文字列を数値に変換

valueOfメソッドは文字列を数値に変換することもできます。

public class Sample {
	public static void main(String[] args) {

		String str1 = "2021";
		int a = Integer.valueOf(str1); //int型に変換
		System.out.println(a);	// 2021

		long b = Long.valueOf(str1); //long型に変換
		System.out.println(b);	// 2021

		String str2 = "2021.21";

		float c = Float.valueOf(str2); //float型に変換
		System.out.println(c);	// 2021.21

		String str3 = "2021.21";

		double d = Double.valueOf(str3); //double型に変換
		System.out.println(d);	// 2021.21

	}
}

参照

Java Platform SE8 クラスString

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?