0
0
PowerShellscripting~5 mins

Integer and floating-point types in PowerShell

Choose your learning style9 modes available
Introduction
Integer and floating-point types let you work with whole numbers and numbers with decimals in your scripts.
When you need to count items, like files or users (use integers).
When you want to measure something that can have fractions, like temperature or price (use floating-point).
When doing math calculations that require decimals, like averages or percentages.
When comparing numbers to make decisions in your script.
When storing numeric data that will be used later in your script.
Syntax
PowerShell
# Integer example
$wholeNumber = 10

# Floating-point example
$decimalNumber = 3.14
Integers are whole numbers without decimals.
Floating-point numbers have decimals and can represent fractions.
Examples
Here, $age is an integer and $pi is a floating-point number.
PowerShell
$age = 25
$pi = 3.14159
You can explicitly set the type using [int] for integers and [double] for floating-point numbers.
PowerShell
$count = [int]100
$price = [double]19.99
Division of integers results in a floating-point number in PowerShell.
PowerShell
$result = 5 / 2
Write-Output $result
Sample Program
This script shows how to use integer and floating-point numbers and adds them together.
PowerShell
$wholeNumber = 7
$floatingNumber = 2.5
$sum = $wholeNumber + $floatingNumber
Write-Output "Whole number: $wholeNumber"
Write-Output "Floating number: $floatingNumber"
Write-Output "Sum: $sum"
OutputSuccess
Important Notes
PowerShell automatically treats numbers with decimals as floating-point.
You can convert between types using [int] or [double] if needed.
Be careful with floating-point precision in calculations.
Summary
Integers are for whole numbers, floating-point for decimals.
PowerShell handles math with both types easily.
Use explicit type casting when you want to control the number type.