PowerShell: How to Get the Fractional Part of a Number

Simple techniques for splitting decimals and integers

Method 1: The Modulo Operator (Best for Math)

The simplest mathematical way to get a fractional remainder is using the modulo operator %, but since PowerShell's standard modulo is for integers, we use simple subtraction logic.

Logic: Number - Floor(Number)

$num = 12.3456 $fraction = $num - [Math]::Floor($num) Write-Host $fraction # Output: 0.3456

Method 2: String Splitting (Best for Text Preservation)

If you want the exact digits without floating point rounding errors (e.g., "0.300"), treating the number as a string is safer.

$num = "12.3456" $parts = $num.Split(".") Write-Host "Whole:" $parts[0] Write-Host "Fraction:" $parts[1] # Output Fraction: 3456
Warning: This method depends on the locale! In some countries, a comma is used instead of a dot.

Method 3: Cast to Integer

You can also cast the number to an [int] (be careful with rounding) to subtract it.

$num = 12.34 $fraction = $num - [int]$num # Be careful! [int] rounds to nearest even number in PowerShell by default.

It is safer to use [Math]::Truncate():

$fraction = $num - [Math]::Truncate($num)

Related Topics