0
0
PowerShellscripting~10 mins

Integer and floating-point types in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Integer and floating-point types
Start
Declare variable
Assign integer value?
YesVariable stores integer
Use integer operations
Assign floating-point value?
YesVariable stores float
Use float operations
Error or other type
End
This flow shows how a variable in PowerShell can hold either an integer or a floating-point number depending on the assigned value, and how operations depend on the type.
Execution Sample
PowerShell
$intVar = 10
$floatVar = 3.14
$intVarType = $intVar.GetType().Name
$floatVarType = $floatVar.GetType().Name
Write-Output "$intVarType, $floatVarType"
This script assigns an integer and a floating-point number to variables, then outputs their types.
Execution Table
StepActionVariableValueTypeOutput
1Assign 10 to $intVar$intVar10Int32
2Assign 3.14 to $floatVar$floatVar3.14Double
3Get type of $intVar$intVarTypeInt32String
4Get type of $floatVar$floatVarTypeDoubleString
5Output typesInt32, Double
💡 Script ends after outputting the types of both variables.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
$intVarnull1010101010
$floatVarnullnull3.143.143.143.14
$intVarTypenullnullnullInt32Int32Int32
$floatVarTypenullnullnullnullDoubleDouble
Key Moments - 2 Insights
Why does $intVar have type Int32 and $floatVar have type Double?
Because PowerShell automatically assigns the type based on the value: whole numbers become Int32, and numbers with decimals become Double, as shown in steps 1 and 2 of the execution_table.
Can I perform math operations between $intVar and $floatVar without converting types?
Yes, PowerShell will handle mixed-type math by converting integers to floating-point as needed, but the variable types remain as assigned, as seen in the variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the type of $floatVar after step 2?
AInt32
BString
CDouble
DFloat32
💡 Hint
Check the 'Type' column for $floatVar in step 2 of the execution_table.
At which step does $intVarType get assigned a value?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Variable' and 'Action' columns in the execution_table to see when $intVarType is set.
If you assign 5.0 to $intVar instead of 10, what type would $intVar have?
AInt32
BDouble
CString
DDecimal
💡 Hint
Recall that numbers with decimals become Double, as shown for $floatVar in the execution_table.
Concept Snapshot
PowerShell variables can hold integers (Int32) or floating-point numbers (Double).
Assigning a whole number stores Int32; assigning a decimal stores Double.
Use .GetType().Name to check variable type.
Math operations auto-convert types as needed.
Understanding types helps avoid errors in calculations.
Full Transcript
This lesson shows how PowerShell handles integer and floating-point types. Variables assigned whole numbers become Int32 type, while those assigned decimal numbers become Double type. The example script assigns 10 to $intVar and 3.14 to $floatVar, then outputs their types. The execution table traces each step, showing variable values and types. Key moments clarify why types differ and how PowerShell manages math between them. The visual quiz tests understanding of variable types and assignment steps. Remember, PowerShell automatically chooses the type based on the value assigned.