kotlin エラー「error: the integer literal does not conform to the expected type Double」の解決方法

kotlin エラー「error: the integer literal does not conform to the expected type Double」の解決方法

kotlinで、エラー「error: the integer literal does not conform to the expected type Double」の解決方法を記述してます。浮動小数点数の型に対して整数を指定した場合などに発生します。

環境

  • OS windows11 home
  • java 17.0.2
  • kotlin 1.6.10-release-923

エラー全文

以下のコードで発生。

fun main() {

    var x:Double = 10

}

エラーメッセージ

error: the integer literal does not conform to the expected type Double
    var x:Double = 10.0
                  ^

原因

整数は、浮動小数点数として認識しないため

対処法

「Int」型などを使用する

fun main() {

    var x:Int = 10

}
説明
Double64ビット浮動小数点数
Float32ビット浮動小数点数
Long64ビット符号付き整数
Int32ビット符号付き整数
Short16ビット符号付き整数
Byte8ビット符号付き整数