Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Go 浮点数数据类型


Go 浮点数数据类型

浮点数据类型用于存储带小数点的正数和负数,例如 35.3、-2.34 或 3597.34987。

浮点数据类型有两个关键字

类型 大小 范围
float32 32 位 -3.4e+38 到 3.4e+38。
float64 64 位 -1.7e+308 到 +1.7e+308。

提示:浮点数的默认类型是 float64。如果未指定类型,则类型将为 float64


float32 关键字

示例

此示例演示如何声明一些 float32 类型的变量。

package main
import ("fmt")

func main() {
  var x float32 = 123.78
  var y float32 = 3.4e+38
  fmt.Printf("类型: %T, 值: %v\n", x, x)
  fmt.Printf("类型: %T, 值: %v", y, y)
}
亲自尝试 »

float64 关键字

float64 数据类型可以存储比 float32 更大的数字集。

示例

此示例演示如何声明一个 float64 类型的变量。

package main
import ("fmt")

func main() {
  var x float64 = 1.7e+308
  fmt.Printf("类型: %T, 值: %v", x, x)
}
亲自尝试 »

使用哪种浮点类型?

要选择的浮点类型取决于变量需要存储的值。

示例

此示例将导致错误,因为 3.4e+39 超出了 float32 的范围。

package main
import ("fmt")

func main() {
  var x float32= 3.4e+39
  fmt.Println(x)
}

结果

./prog.go:5:7: 常量 3.4e+39 超出 float32 范围

亲自尝试 »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.