Powershell: Zasady budowania Format string podczas formatowania wartości liczbowych

8-paź-2016

Ten temat poruszałem już kilka razy na blogu, ale ciągle pojawiają się nowe pytania. Widać nie tylko ja miewam z tym kłopoty. Inne artykuly z tego bloga na ten i podobne tematy:

Kiedy chcesz przygotować instrukcję, która ma wyświetlić tekst z wartościami liczbowymi, datami itp. możesz skorzystać ze składni:

"This is the first number {0} and this is the second number {1}" -f 1234.567,2345.678

Wynik to:

This is the first number 1234,567 and this is the second number 2345,678

albo:

"This is the first number {0,10:0.00} and this is the second number {1:#.000}" -f 1234.567,2345.678

wynik to:

This is the first number    1234,57 and this is the second number 2345,678

Ogólnie należy ułożyć napis, a w miejscach gdzie mają się pojawić wartości liczbowe umieścić placeholder, który określa który z parametrów po parametrze -f ma być tu umieszczony i z jakim formatowaniem. Oto opis składni stosowanej w placeholder:

{index[,alignment][:formatString]}

  • index definiuje o której zmiennej przekazywanej przez parametr -f jest mowa
  • alignment jest wartością opcjonalną i definiuje wyrównanie. Jest to liczba znaków, jaką może zajmować sformatowana zmienna. Jeśli liczba jest dodatnia to wartość będzie wyrównana do prawej strony, a z przodu uzupełniona spacjami. Jeśli liczba jest ujemna to napis będzie wyrównany do lewej i uzupełniony spacjami z tyłu.
  • formatString również jest opcjonalny i pozwala na określenie specyficznego formatowania jakie ma być zastosowane, np. ilość cyfr po przecinku, uzupełnienie zerami lub określenie sposobu formatowania daty

Oto kolejne przykłady

„{0,6}” -f 4.90
4.99

„{0,6:##.00}” -f 5.9
15.90

Podczas formatowania można korzystać z uproszczonego formatowania standardowego lub elastycznego własnego formatowania (custom)

Poniższe przykłady pochodzą z https://www.safaribooksonline.com/library/view/windows-powershell-pocket/9781449363369/ch04.html

Znaczenie i wykaz napisów formatujących można znaleźć też pod adresami:

Formatowanie standardowe

C or c
Currency
A currency amount.

„{0:C}” -f 1.23
1,23 zł

D or d
Decimal
A decimal amount (for integral types). The precision specifier controls the minimum number of digits in the result.

„{0:D4}” -f 2
0002

E or e
Scientific
Scientific (exponential) notation. The precision specifier controls the number of digits past the decimal point.

„{0:E3}” -f [Math]::Pi
3.142E+000

F or f
Fixed-point
Fixed-point notation. The precision specifier controls the number of digits past the decimal point.

„{0:F3}” -f [Math]::Pi
3.142

G or g
General
The most compact representation (between fixed-point and scientific) of the number. The precision specifier controls the number of significant digits.

„{0:G3}” -f [Math]::Pi
3.14
„{0:G3}” -f 1mb
1.05E+06

N or n
Number
The human-readable form of the number, which includes separators between number groups. The precision specifier controls the number of digits past the decimal point.

„{0:N4}” -f 1mb
1,048,576.0000

P or p
Percent
The number (generally between 0 and 1) represented as a percentage. The precision specifier controls the number of digits past the decimal point.

„{0:P4}” -f 0.67
67.0000

R or r
Roundtrip
The Single or Double number formatted with a precision that guarantees the string (when parsed) will result in the original number again.

„{0:R}” -f (1mb/2.0)
524288
„{0:R}” -f (1mb/9.0)
116508.44444444444

X or x
Hexadecimal
The number converted to a string of hexadecimal digits. The case of the specifier controls the case of the resulting hexadecimal digits. The precision specifier controls the minimum number of digits in the resulting string.

„{0:X4}” -f 1324
052C

Własne formaty (custom)

0
Zero placeholder
Specifies the precision and width of a number string. Zeros not matched by digits in the original number are output as zeros.

„{0:00.0}” -f 4.12341234
04.1

#
Digit placeholder
Specifies the precision and width of a number string. # symbols not matched by digits in the input number are not output.

„{0:##.#}” -f 4.12341234
4.1

.
Decimal point
Determines the location of the decimal.

„{0:##.#}” -f 4.12341234
4.1

,
Thousands separator
When placed between a zero or digit placeholder before the decimal point in a formatting string, adds the separator character between number groups.

„{0:#,#.#}” -f 1234.121234
1,234.1

,
Number scaling
When placed before the literal (or implicit) decimal point in a formatting string, divides the input by 1,000. You can apply this format specifier more than once.

„{0:##,,.000}” -f 1048576
1.049

%
Percentage placeholder
Multiplies the input by 100, and inserts the percent sign where shown in the format specifier.

„{0:%##.000}” -f .68
%68.000

E0
E+0
E-0
e0
e+0
e-0

Scientific notation
Displays the input in scientific notation. The number of zeros that follow the E define the minimum length of the exponent field.

„{0:##.#E000}” -f 2.71828
27.2E-001

’ text ’
” text ”
Literal string
Inserts the provided text literally into the output without affecting formatting.

„{0:#.00’zł’}” -f 2.71828
2.72zł

;
Section separator
Allows for conditional formatting.
If your format specifier contains no section separators, the formatting statement applies to all input.
If your format specifier contains one separator (creating two sections), the first section applies to positive numbers and zero, and the second section applies to negative numbers.
If your format specifier contains two separators (creating three sections), the sections apply to positive numbers, negative numbers, and zero.

„{0:POS;NEG;ZERO}” -f -14
NEG

Other
Other character
Inserts the provided text literally into the output without affecting formatting.

„{0:$## Please}” -f 14
$14 Please

Komentarze są wyłączone

Autor: Rafał Kraik