23
8

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 5 years have passed since last update.

Go : float64からintにキャストすると切り捨てになる

Posted at

float64からintにキャストすると切り捨てされる ※実行結果[2]参照

main.go
package main

import "fmt"

func main() {
	a := 2.0 / 3.0 * 100 // float64になる
	fmt.Println("[1] float64 で出力 :", a)
	fmt.Println("[2] int で出力     :", int(a))	
	b := int(a) // float64からintにキャスト
	fmt.Println("[3] float64 で出力1:", float64(b))
	fmt.Printf("[4] float64 で出力2: %f\n", float64(b))
}

実行結果

[1] float64 で出力 : 66.66666666666667
[2] int で出力     : 66
[3] float64 で出力1: 66
[4] float64 で出力2: 66.000000
23
8
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
23
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?