»Format« – Variable Anzahl von Argumenten in C# [] (»Format« – Variable Anzahl von Argumenten in C#), Lektion, Seite 723807
https://www.purl.org/stefan_ram/pub/variable_anzahl_csharp (Permalink) ist die kanonische URI dieser Seite.
Stefan Ram
C#-Kurs

Variable Anzahl von Argumenten in C♯ 

Die „Formatierung“ einer Zeichenfolge mit »global::System.String.Format« ergibt in vielen Fällen wieder diese Zeichenfolge.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format( "abc" )); }}
Protokoll
abc

Aufruf als Operand

Das Ergebnis eines Aufrufs von »global::System.String.Format« kann also auch wieder als String-Operand verwendet werden.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( "x" + global::System.String.Format( "abc" )+ 2 ); }}
Protokoll
xabc2

Vararg-Parameter

Es handelt sich bei »global::System.String.Format« jedoch um eine „Vararg“-Methode. Das heißt, sie kann mit fast beliebig vielen  Argumenten aufgerufen werden – es muß mindestens ein Argument geben, und das erste Argument muß eine Zeichenfolge sein.

Die weiteren Argumente, die wir auch Zusatzargumente  nennen, werden jedoch zunächst ignoriert.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format( "alpha", "gamma", "delta" )); }}
Protokoll
alpha

Das folgende Programmbeispiel zeigt, daß die Zusatzargumente auch numerisch  sein dürfen.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( "alpha",
1, 1.2, 3m, global::Microsoft.JScript.MathObject.random() )); }}
Protokoll
alpha

Texteinsetzungen

Die Verwendung eines ganzzahligen Numerales in geschweiften Klammern wird durch die Textdarstellung eines Zusatzarguments ersetzt. Führende Nullen sind dabei bei positiven Werten nicht erlaubt. Die Numerierung der Zusatzargumente beginnt bei 0. Wir bezeichnen den angegebenen Wert als als Versatz.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format( "alpha{0}", "gamma" )); }} /*
^ |
'-------' */
Protokoll
alphagamma
Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format( "alpha{0}{1}", "gamma", "delta" )); }} /*
^ ^ | |
'--|-------' |
'----------------' */
Protokoll
alphagammadelta
Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format( "---{1}...{0}===", "gamma", "delta" )); }} /*
^ ^ | |
| '----------' |
'-------------------------' */
Protokoll
---delta...gamma===
Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( "a{0}b{0}c", global::System.Environment.NewLine )); }}/*
^ ^ |
'---'-----------------------' */
Protokoll
a
b
c

Nachkommastellen

Durch ein mit einem Doppelpunkt abgegrenztes Format, welches dem Versatze nachgestellt wird, kann die Darstellung eines Zusatzarguments beeinflußt werden.

Das Format »F« verlangt eine Darstellung einer Gleitkommazahl ohne die E-Schreibweise.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( "{0}{1}{0:F}", 0.000001, global::System.Environment.NewLine )); }}
Protokoll
1E-06
0.00

Unter einigen Umgebungen erscheint in der Ausgabe ein Dezimalkomma »,« statt eines Dezimalpunktes ».« (wenn der Rechner entsprechend eingestellt wurde).

Das Format »F8« verlangt eine Darstellung wie bei »F«, aber mit acht Nachkommastellen.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( "{0:F8}", 0.000001 )); }}
Protokoll
0.00000100

Das Format »F8« verlangt eine Darstellung wie bei »F«, aber ohne Nachkommastellen.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( "{0:F0}", 0.000001 )); }}
Protokoll
0

Das Format »F2« verlangt eine Darstellung wie bei »F«, aber mit zwei Nachkommastellen. Man kann erkennen, daß hierbei gerundet wird.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( "{0:F2}, {1:F2}, {2:F2}", 0.854, 0.855, 0.856 )); }}
Protokoll
0.85, 0.86, 0.86

Eine C♯ -Implementation macht manchmal kleine Rechenfehler. So ergibt »120 * 1.11« beispielsweise nicht den schulmathematisch korrekten Wert »133.2«, sondern eher den Wert »133.20000000000002«. Diese Rechenfehler werden aber bei der Ausgabe versteckt und sind daher nicht ohne weiteres sichtbar.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( 120 * 1.11 ); }}
Protokoll
133.2

Selbst, wenn man ausdrücklich 14 Stellen verlangt, erscheint die »2« am Ende nicht.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( "{0:F14}", 120 * 1.11 )); }}
Protokoll
133.20000000000000

Die Abweichung in der 14. Nachkommastelle kann aber durch Bildung der Differenz gezeigt werden.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( "{0:F14}", 120 * 1.11 - 133.20 )); }}
Protokoll
0.00000000000003

Das spezielle Format »R« erlaubt es schließlich, den wahren Wert des Ergebnisses zu sehen.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( "{0:R14}", 120 * 1.11 )); }}
Protokoll
133.20000000000002

Bei dem Format »F« werden Werte, die nur geringfügig von einem Wert mit viel weniger Nachkommastellen abweichen, manchmal etwas „geschönt“, indem die Abweichungen versteckt werden.

Angabe einer Umgebung

Das Format »F« verwendet ein Dezimaltrennzeichen, das den Einstellung der verwendeten Umgebung entspricht. Statt dessen kann bestimmte Kultur auch ausdrücklich als erstes Argument übergeben werden.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( global::System.Globalization.CultureInfo.CreateSpecificCulture( "de-DE" ),
"{0:F8}", 0.000001 )); }}
Protokoll
0,00000100
Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( global::System.Globalization.CultureInfo.CreateSpecificCulture( "en-US" ),
"{0:F8}", 0.000001 )); }}
Protokoll
0.00000100

Darstellungen ganzer Zahlen in verschiedenen Zahlensystemen

Das Format »F« verwendet ein Dezimaltrennzeichen, das den Einstellung der verwendeten Umgebung entspricht. Statt dessen kann bestimmte Kultur auch ausdrücklich als erstes Argument übergeben werden.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( "{0:D}", 123 )); }}
Protokoll
123
Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( "{0:X}", 123 )); }}
Protokoll
7B

Darstellung von Klammern »{«

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( "{{A}}" )); }}
Protokoll
{A}

Tausendertrennzeichen

In einem „benutzerdefinierten Format“ verlangen Nummernzeichen »#« vor einem Punkt bestimmte Anzahl von Nachkommastellen.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( "{0:|####.####}", 12.34 )); }}
Protokoll
|12.34
Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( "{0:|####.####}", 123456789.0 )); }}
Protokoll
|123456789

Nummernzeichen »#« nach einem Punkt begrenzen die Anzahl von Stellen.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( "{0:|####.####}", 0.123456789 )); }}
Protokoll
|.1235

Wird an Stellen eines Nummernzeichens eine »0« verwendet, so werden so viele Nullen wie angegeben verwendet.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( "{0:|0000.0000}", 12.34 )); }}
Protokoll
|0012.3400

Ein Komma vor dem Punkt verlangt die Verwendug einer Tausendertrennung. Das Komma muß sich zwischen zwei Ziffernsymbolen (»#« oder »0«) befinden.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( "{0:#,#.000}", 123456789 )); }}
Protokoll
123.456.789,000
Protokoll
123,456,789.000

Feldbreiten

Mit einem Komma abgetrennt kann eine Feldbreite hinter dem Versatz angegeben werden. Es wird dann mit Leerzeichen auf die Feldbreite aufgefüllt.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( "]{0,2}[", 1 )); }}
Protokoll
] 1[

Die Angaben einer Feldbreite und die weiter oben vorgestellten Formate können auch kombiniert werden.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( "]{0,9:F3}[", 12.3456789 )); }}
Protokoll
]   12.346[

Die Angaben einer negativen Feldbreite führt zu linksbündiger Ausrichtung.

Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine
( global::System.String.Format
( "]{0,-9:F3}[", 12.3456789 )); }}
Protokoll
]12.346   [

Übersicht

Diese Lektion hat nur beispielhaft einige der Formatierungsmöglichkeiten und Regeln der Methode »global::System.String.Format« herausgegriffen. Eine vollständige Darstellung aller Möglichkeiten jener Methode findet man in ihrer Dokumentation.

Hier folgt noch einmal eine Übersicht der in dieser Lektion vorgestellten Beispiele.

geschweifte Klammern und Formatierungseinträge
{{          geschweifte Klammer auf
}} geschweifte Klammer zu
{0} Einfuegen von Zusatzargument 0
{0:F} Einfuegen von Zusatzargument 0 mit dem Format F
{0,2} Auffuellen auf ein Feld der Breite 2
{0,9:F3} Kombination von Feldbreite 9 und 3 Nachkommastellen
{0,-9:F3} linksbuendige Ausrichtunb
Formate hinter dem Doppelpunkt
F           Festkomma
F8 Festkomma mit 8 Nachkommastellen
F0 Festkomma ohne Nachkommastellen
F14 Festkomma mit 14 Nachkommastellen
R ungeschoentes Festkomma mit 14 Nachkommastellen
D Dezimalsystem
X Hexadezimalsystem mit großen Buchstaben
|####.#### senkrechter Strich und maximal vier Nachkommastellen
0000.0000 mindestens vier Vorkomma- und genau vier Nachkommastellen
#,#.000 Verwendung von Tausendertrennung

Seiteninformationen und Impressum   |   Mitteilungsformular  |   "ram@zedat.fu-berlin.de" (ohne die Anführungszeichen) ist die Netzpostadresse von Stefan Ram.   |   Eine Verbindung zur Stefan-Ram-Startseite befindet sich oben auf dieser Seite hinter dem Text "Stefan Ram".)  |   Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram. Schlüsselwörter zu dieser Seite/relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram723807 stefan_ram:723807 »Format« – Variable Anzahl von Argumenten in C# Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd723807, slrprddef723807, PbclevtugFgrsnaEnz Erklärung, Beschreibung, Info, Information, Hinweis,

Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram.
https://www.purl.org/stefan_ram/pub/variable_anzahl_csharp