Tinus EngOps Wiki

Logo

OpsaC - Operating as PowerShell code

Links

Home

IT-Wiki

PowerShell Blog

PowerShell Index

PowerShell Search

Additional Websites

View my GitHub Profile

View my GitHub Gists

View my Codeberg Page

View my Photo Website

Format string

published: September 20, 2017 author: Tinu tags: Format - Random categories: PowerShell


.NET Framework formatting methods

Einfache .NET Framework Methode zum Formatieren von Zahlen.

"{0:N2}" -f 554.22272, 2345555

0 = Index number of the item to be formatted
N = Datentyp (Numeric, Digits, Currency, Percentage, HeXadecimal, 0 = leading zero)
2 = Anzahl Nachkommastellen

More Fun With Formatting Numbers

Variable mit einer Nummer abfüllen:

$number = 6348

Numeric mit zwei Nachkommastellen

"{0:N2}" -f $number

Output

6'348.00

The value is displayed using the number of digits; if needed, leading zeroes are added to the beginning of the number.

"{0:D8}" -f $number

Output

00006348

Währung mit zwei Nachkommastellen

"{0:C2}" -f $number

Output

CHF 6'348.00

The value is multiplied by 100 and displayed as a percentage.

"{0:P0}" -f $number

Output

634'800%

The value is displayed as a hexadecimal number.

"{0:X0}" -f $number

Output

18CC

Leading zeroes

Randomize Zeit zusammenstellen:

"{0:d2}" -f (Get-Random -Maximum 23) + ":" + "{0:d2}" -f (Get-Random -Maximum 59) + ":" + "{0:d2}" -f (Get-Random -Maximum 59)

Output

08:38:55

IBAN oder so was ähnliches generieren:

"{0:CH00}" -f (Get-Random -Maximum 99) + " " + "{0:0000}" -f (Get-Random -Maximum 9999) + " " + "{0:0000}" -f (Get-Random -Maximum 9999) + " " + "{0:0000}" -f (Get-Random -Maximum 9999)

Output

CH68 1706 6845 5030

1000 Dateien anlegen:

1..1000 | ForEach-Object { 
    $FileName = "Test_{0:d4}.txt" -f $_
    New-Item -ItemType File -Name $FileName -WhatIf
}

Zeitangaben formatieren

Will man eine Zeitspanne formatiert ausgeben, kann dies wie folgt gemacht werden:

$(New-TimeSpan -Start ((Get-Date).AddDays(-3)) -End (Get-Date) | % { "{0:0d} {1:0}h {2:0}m {3:0}s {4:000}ms" -f $_.Days, $_.Hours, $_.Minutes, $_.Seconds, $_.Milliseconds })

Output

3d 0h 0m 0s 001ms

← Previous Post ◇ [ Top ] ◇ Copyright © 2024 by tinuwalther ◇ [ Blog ] ◇ Next Post →