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
}
型 | 説明 |
---|---|
Double | 64ビット浮動小数点数 |
Float | 32ビット浮動小数点数 |
Long | 64ビット符号付き整数 |
Int | 32ビット符号付き整数 |
Short | 16ビット符号付き整数 |
Byte | 8ビット符号付き整数 |
-
前の記事
JavaScriptでのクロスブラウザ開発:最新の互換性を保つためのベストプラクティス 2025.03.24
-
次の記事
Reactでダークモードを実装する方法 2025.03.25
コメントを書く