「Java」String型から数値型(float、double、int、boolean)に変換する方法

2020年10月19日

説明
1.public static int parseInt(String s) throws NumberFormatException
文字列の引数を符号付き10進数の整数型として構文解析します。
2.public static float parseFloat(String s) throws NumberFormatException
FloatクラスのvalueOfメソッドを実行した場合と同様に、
指定されたStringが表す値に初期化された新しいfloat値を返します。
Javaコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
public class CastDemo {
public static void main(String[] args) {
// string->int
int in = Integer.parseInt("678");
// string->long
long ln = Long.parseLong("12345");
// string->float
float fl = Float.parseFloat("7788");
// string->double
double db = Double.parseDouble("9900");
// string->boolean
boolean bo = Boolean.parseBoolean("true");
// string->byte
byte by = Byte.parseByte("8");
// string->short
short sh = Short.parseShort("925");
// 変換結果
System.out.println("数値文字列->int型: " + in);
System.out.println("数値文字列->long: " + ln);
System.out.println("数値文字列->float: " + fl);
System.out.println("数値文字列->double: " + db);
System.out.println("数値文字列->boolean: " + bo);
System.out.println("数値文字列->byte: " + by);
System.out.println("数値文字列->short: " + sh);
}
}
package com.arkgame.study; public class CastDemo { public static void main(String[] args) { // string->int int in = Integer.parseInt("678"); // string->long long ln = Long.parseLong("12345"); // string->float float fl = Float.parseFloat("7788"); // string->double double db = Double.parseDouble("9900"); // string->boolean boolean bo = Boolean.parseBoolean("true"); // string->byte byte by = Byte.parseByte("8"); // string->short short sh = Short.parseShort("925"); // 変換結果 System.out.println("数値文字列->int型: " + in); System.out.println("数値文字列->long: " + ln); System.out.println("数値文字列->float: " + fl); System.out.println("数値文字列->double: " + db); System.out.println("数値文字列->boolean: " + bo); System.out.println("数値文字列->byte: " + by); System.out.println("数値文字列->short: " + sh); } }
package com.arkgame.study;

public class CastDemo {

      public static void main(String[] args) {
            // string->int
            int in = Integer.parseInt("678");
            // string->long
            long ln = Long.parseLong("12345");
            // string->float
            float fl = Float.parseFloat("7788");
            // string->double
            double db = Double.parseDouble("9900");
            // string->boolean
            boolean bo = Boolean.parseBoolean("true");
            // string->byte
            byte by = Byte.parseByte("8");
            // string->short
            short sh = Short.parseShort("925");

            // 変換結果
            System.out.println("数値文字列->int型: " + in);
            System.out.println("数値文字列->long: " + ln);
            System.out.println("数値文字列->float: " + fl);
            System.out.println("数値文字列->double: " + db);
            System.out.println("数値文字列->boolean: " + bo);
            System.out.println("数値文字列->byte: " + by);
            System.out.println("数値文字列->short: " + sh);

      }

}

結果
数値文字列->int型: 678
数値文字列->long: 12345
数値文字列->float: 7788.0
数値文字列->double: 9900.0
数値文字列->boolean: true
数値文字列->byte: 8
数値文字列->short: 925

Java

Posted by arkgame