Floating-Point Numbers
Part of the Fundamentals section of Coddy's GO journey — lesson 8 of 109.
Let's learn about floating-point numbers in Go, which we use to store decimal values.
First, let's declare a floating-point variable with an explicit type:
var price float64 = 19.99
fmt.Println("Price:", price)After executing this code, you'll see the following on the screen:
Price: 19.99We've created a variable that can hold decimal numbers.
In Go, there are two floating-point types: float32 and float64. Let's see how to use float32:
var temperature float32 = 23.5
fmt.Println("Temperature:", temperature)After executing this code, you'll see the following on the screen:
Temperature: 23.5The difference between float32 and float64 is precision and range. float64 uses 64 bits and provides higher precision (about 15-17 decimal digits) while float32 uses 32 bits with less precision (about 6-9 decimal digits).
For most applications, float64 is recommended as it's more precise.
Now, let's use Go's type inference to create a floating-point number more simply:
temperature := 23.5 // Go automatically uses float64
fmt.Println("Temperature:", temperature)After executing this code, you'll see the following on the screen:
Temperature: 23.5When you include a decimal point in a number, Go automatically treats it as a float64 type.
Let's try some basic math with floating-point numbers:
radius := 5.0
pi := 3.14159
area := pi * radius * radius
fmt.Println("Circle area:", area)After executing this code, you'll see the following on the screen:
Circle area: 78.53975Floating-point numbers allow us to perform calculations with decimal values.
Challenge
BeginnerIn this challenge, you'll practice working with floating-point numbers in Go.
Floating-point numbers are used to represent decimal values in Go. There are two main types: float32 and float64, with float64 being more commonly used as it provides greater precision.
Your task is to declare a floating-point variable and print it to the console.
Cheat sheet
Go has two floating-point types for decimal numbers:
float32- 32 bits, less precision (6-9 decimal digits)float64- 64 bits, higher precision (15-17 decimal digits), recommended for most applications
Declare floating-point variables with explicit type:
var price float64 = 19.99
var temperature float32 = 23.5Use type inference with decimal point (Go automatically uses float64):
temperature := 23.5Perform calculations with floating-point numbers:
radius := 5.0
pi := 3.14159
area := pi * radius * radiusTry it yourself
package main
import "fmt"
func main() {
// Some examples of floating-point numbers
pi := 3.14159
temperature := 72.5
// TODO: Declare a new float64 variable named 'height' with the value 5.9
height := ?
// Print the existing variables
fmt.Println("Pi approximately equals:", pi)
fmt.Println("The temperature is:", temperature)
// TODO: Print your new 'height' variable with a descriptive message
fmt.Println("The height is:", ?)
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions