Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How to Display the Output with Two Decimals in PowerShell

June 7, 2024
in Blog, Powershell
0
ADVERTISEMENT

Table of Contents

Display the Output with Two Decimals in PowerShell

In this post, when checking the size of a folder using PowerShell. The output shows the size like the below example. In some cases, you want to make it shorter for readability.

$path = 'C:\Program Files\WindowsPowerShell\Modules\ExchangeOnlineManagement\3.3.0'
$folderSize = (Get-ChildItem $path -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB
PS C:\> $folderSize
34.1878614425659

There are two ways to do it as follow.

pooIqykOSADcTjtpxxCzj3Nfyx95qnKbFFTiStTDXdAFBSvefcKS4hJarZDz

Use the -f format operator

The first way, we can use the -f format operator to display the output with two decimals only.

$path = 'C:\Program Files\WindowsPowerShell\Modules\ExchangeOnlineManagement\3.3.0'
$folderSize = (Get-ChildItem $path -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB

$("{0:N2}" -f $folderSize)
# Output
34.19

In -f format operator we provide the strings and numbers on the right-hand side and syntax on the left-hand side. Below are some examples.

# Display a number to 3 decimal places :
"{0:n3}" -f 123.45678
123.457

# Right align the first number only :
"{0,10}" -f 4,5,6
         4

# Left and right align text :
"|{0,-10}| |{1,10}|" -f "hello", "world"
|hello     ||     world|

# Display an integer with 3 digits :
"{0:n3}" -f [int32]12
012

# Separate a number with dashes (# digit place holder):
"{0:###-##-##}" -f 1234567
123-45-67

#Display a number as a percentage:
"{0:p0}" -f 0.5
50%

# Display a whole number padded to 5 digits:
"{0:d5}" -f 123
00123

Use the type math

The second way we can use the type math with its static method round() method.

$path = 'C:\Program Files\WindowsPowerShell\Modules\ExchangeOnlineManagement\3.3.0'
$folderSize = (Get-ChildItem $path -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB

$([math]::round($folderSize,2))
# Output
34.19

In case you want to display the output with four decimals instead of two decimals

$path = 'C:\Program Files\WindowsPowerShell\Modules\ExchangeOnlineManagement\3.3.0'
$folderSize = (Get-ChildItem $path -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB

$([math]::round($folderSize,4))
# Output
34.1879
ADVERTISEMENT

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

How to Export Licensed Users with Last Sign-in Time in Microsoft 365

Next Post

How to Fix Cannot Install New Microsoft Teams Because It Uses Certain Restricted Capabilities

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory