Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1563 | Anzahl registrierter User: 1895 | Besucher online: 110
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

Römische Zahlen <-> Int32


Autor: Jan Oliver Rüdiger
Sprache: C#
Bewertung: 5,8
(1 Bewertung)
Anzahl der Aufrufe: 2558
  
Kick it on dotnet-kicks.de  

Beschreibung:

Wandelt römische Zahlen in das dezimale Zahlensystem um und umgekehrt.
Int32 -> Römische Notation (inkl. Rundungsregeln)
Römische Notation -> Int32 (Konvertierung auch bei fehlerhafter Notation) [Bsp: XCVV = 100]


Abgelegt unter: Konvertierung, römische Zahlen.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public static class RomeInt32Converter
  {
    public static string ToRome(int value)
    {
      if ((value < 1) || (value >= Int32.MaxValue)) { return ""; }
      string res = "";

      while (value >= 1000) { value -= 1000; res += "M"; }
      if (value >= 900) { value -= 900; res += "CM"; }

      while (value >= 500) { value -= 500; res += "D"; }
      if (value >= 400) { value -= 400; res += "CD"; }

      while (value >= 100) { value -= 100; res += "C"; }
      if (value >= 90) { value -= 90; res += "XC"; }

      while (value >= 50) { value -= 50; res += "L"; }
      if (value >= 40) { value -= 40; res += "XL"; }

      while (value >= 10) { value -= 10; res += "X"; }
      if (value >= 9) { value -= 9; res += "IX"; }

      while (value >= 5) { value -= 5; res += "V"; }
      if (value >= 4) { value -= 4; res += "IV"; }

      while (value >= 1) { value -= 1; res += "I"; }

      return res;
    }

    public static int ToDecimal(string value)
    {
      if (String.IsNullOrEmpty(value)) { return 0; }
      int res = 0;

      while (value.StartsWith("M")) { res += 1000; value = value.Substring(1); }
      if (value.StartsWith("CM")) { res += 900; value = value.Substring(2); }

      while (value.StartsWith("D")) { res += 500; value = value.Substring(1); }
      if (value.StartsWith("CD")) { res += 400; value = value.Substring(2); }

      while (value.StartsWith("C")) { res += 100; value = value.Substring(1); }
      if (value.StartsWith("XC")) { res += 90; value = value.Substring(2); }

      while (value.StartsWith("L")) { res += 50; value = value.Substring(1); }
      if (value.StartsWith("XL")) { res += 40; value = value.Substring(2); }

      while (value.StartsWith("X")) { res += 10; value = value.Substring(1); }
      if (value.StartsWith("IX")) { res += 9; value = value.Substring(2); }

      while (value.StartsWith("V")) { res += 5; value = value.Substring(1); }
      if (value.StartsWith("IV")) { res += 4; value = value.Substring(2); }

      while (value.StartsWith("I")) { res += 1; value = value.Substring(1); }

      return res;
    }
  }
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



Diese Snippets könnten für Sie interessant sein:

schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.