Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[C#] String.Format Problem mit Variablen

Empfohlene Antworten

Veröffentlicht

Hallo zusammen,

versuch mich gerade ein wenig an C# und bastle gerade eine Stopuhr. Bei der Formatierung der Anzeige habe ich ein Problem mit führenden Nullen.

Wollte das Ganze mit String.Format lösen, habe allerdings das Problem, dass das String.Format nur genau solange funktioniert, bis ich anstatt einer Zahl die Variable einfüge:

String.Format("{0:D2}", 2) liefert wie gewünscht 02

String.Format("{0:D2}", minutes) liefert solange minutes einstellig ist auch nur einstellig die Ausgabe.

Wo ist mein Denkfehler?

Bearbeitet von Hüsi

Moin!

Ich konnte Dein Problem nicht ganz nachvollziehen.

Ich habe mir einfach mal ein kleines Testprojekt erstellt, mit einem Button und einem Label. Im OnClick des Button steht:


int bla = 2;
label1.Text = String.Format("{0:D2}", bla);
[/PHP]

Wie nicht anders erwartet ;) steht danach "02" im Label.

Welchen Typ hat denn dein "minutes"? Vielleicht liegt es ja daran.

  • Autor

die einzelnen Variablen habe ich als String definiert

private string minutes = "", seconds ="", milliseconds="";
und fülle Sie folgendermaßen:
minutes = ((timer / 60 / 60).ToString());

seconds = (timer / 60 %60).ToString();

milliseconds = (timer %60).ToString();
das Label fütter ich so:
label1.Text = String.Format("{0:D2}", minutes) + ":" + String.Format("{0:D2}", seconds) + ":" + String.Format("{0:D2}", milliseconds);

timer selbst ist Integer.

Dann versuch doch einfach mal String.Format mit den Integer-Werten, statt den Strings zu fuettern.

D.h. deklariere minutes, seconds und milliseconds als integer und lass dann die Umwandlung in den String weg. Dann sollte es funktionieren.


//mit dem (int) vor der Berechnung bin ich mir nicht 100%ig sicher, aber ich meine man muss casten, da er ja FP-Zahlen erhaelt.
int minutes = (int)(timer / 60 / 60);
int seconds = (int)(timer / 60 %60);
int milliseconds = (int)(timer %60);

label1.Text = String.Format("{0:D2}", minutes) + ":" + String.Format("{0:D2}", seconds) + ":" + String.Format("{0:D2}", milliseconds);
[/PHP]

Bearbeitet von Pointerman
Code zugefuegt

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.