📖 Scenario: You are working with numbers in a PowerShell script. Some numbers are whole numbers, and some have decimals. You want to understand how to create and use both integer and floating-point numbers.
🎯 Goal: Learn how to create integer and floating-point variables in PowerShell, and display their values and types.
📋 What You'll Learn
Create an integer variable with a specific value
Create a floating-point variable with a specific value
Display both variables and their types
💡 Why This Matters
🌍 Real World
Scripts often need to work with different kinds of numbers, like counts or measurements with decimals.
💼 Career
Understanding number types helps in writing scripts that handle data correctly in automation tasks.
Progress0 / 4 steps
1
Create an integer variable
Create a variable called wholeNumber and set it to the integer value 42.
PowerShell
Hint
Use = to assign the value 42 to the variable $wholeNumber.
2
Create a floating-point variable
Create a variable called decimalNumber and set it to the floating-point value 3.14.
PowerShell
Hint
Use = to assign the value 3.14 to the variable $decimalNumber.
3
Display the variables and their types
Use Write-Output to display the value of $wholeNumber and its type using GetType().Name. Then do the same for $decimalNumber.
PowerShell
Hint
Use Write-Output with double quotes and $() to show the value and type.
4
Run the script to see the output
Run the script to display the values and types of $wholeNumber and $decimalNumber.
PowerShell
Hint
Run the script in PowerShell. You should see the values and their types printed.
Practice
(1/5)
1. Which data type in PowerShell is used to store whole numbers without decimals?
easy
A. Boolean
B. String
C. Integer
D. Floating-point
Solution
Step 1: Understand data types for numbers
Integers store whole numbers without any decimal part.
Step 2: Identify the correct type for whole numbers
Floating-point types store decimals, strings store text, booleans store true/false values.
A. Casting applies only to $x, division happens after, causing unexpected result
B. Division operator is invalid between int and float
C. Casting $x to int is unnecessary but valid
D. Write-Output cannot print variables
Solution
Step 1: Analyze casting and operation order
[int]$x casts 10 to int (already int), then division by 3.5 happens.
Step 2: Understand result type and unexpected behavior
Division happens after casting, so $z is float 2.857..., not int as might be expected.
Final Answer:
Casting applies only to $x, division happens after, causing unexpected result -> Option A
Quick Check:
Casting affects only $x, division result is float [OK]
Hint: Casting affects only immediate value, not whole expression [OK]
Common Mistakes:
Thinking casting applies to entire expression
Believing division fails between int and float
Assuming Write-Output can't print variables
5. You want to calculate the average of three numbers: 4, 7, and 10 in PowerShell, ensuring the result is a floating-point number. Which script correctly does this?
hard
A.
$avg = ([int](4 + 7 + 10)) / 3 Write-Output $avg
B.
$avg = (4 + 7 + 10) / [int]3 Write-Output $avg
C.
$avg = [int](4 + 7 + 10 / 3) Write-Output $avg
D.
$avg = (4 + 7 + 10) / 3.0 Write-Output $avg
Solution
Step 1: Understand how to get floating-point average
Dividing by a floating-point number (3.0) forces floating-point division.
Step 2: Check each option's effect
$avg = ([int](4 + 7 + 10)) / 3 Write-Output $avg
casts sum to int (already int), division by int 3 gives integer division result truncated.
$avg = [int](4 + 7 + 10 / 3) Write-Output $avg
casts after dividing 10 by 3, changing order and result.