Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

roman

public int RomanToInt(string s)
    {
        if (s == null || s == string.Empty)
            return 0;
        
        Dictionary<string, int> dict = new Dictionary<string, int>();
        int result = 0;
        
        dict.Add("I", 1);
        dict.Add("V", 5);
        dict.Add("X", 10);
        dict.Add("L", 50);
        dict.Add("C", 100);
        dict.Add("D", 500);
        dict.Add("M", 1000);
        dict.Add("IV", 4);
        dict.Add("IX", 9);
        dict.Add("XL", 40);
        dict.Add("XC", 90);
        dict.Add("CD", 400);
        dict.Add("CM", 900);
        
        for (int i = 0; i < s.Length; i++)
            if ((s[i] == 'I' || s[i] == 'X' || s[i] == 'C') && i < s.Length - 1 && dict.ContainsKey(s.Substring(i, 2)))
                result += dict[s.Substring(i++, 2)];
            else
                result += dict[s[i].ToString()];
        
        return result;   
    }
Comment

PREVIOUS NEXT
Code Example
Csharp :: change image of button c# 
Csharp :: count number of properties on an object C# 
Csharp :: c# select first value from list 
Csharp :: c# int array 
Csharp :: how to set a transform equal to something unity 
Csharp :: C# how to use if and else 
Csharp :: c# read last 10 lines of file 
Csharp :: c# console print 
Csharp :: unity deactivate component 
Csharp :: c# return task list 
Csharp :: c# remove rows from datatable 
Csharp :: length of a string c# 
Csharp :: calculate how much memory an object take c# 
Csharp :: hcf of numbers 
Csharp :: c# get certain character from string 
Csharp :: nunjucks index in loop 
Csharp :: get processor id c# web application 
Csharp :: long number multiplication 
Csharp :: c# random 
Csharp :: how to set foreground from code wpf 
Csharp :: basic of c# sockets 
Csharp :: how to check if the value is alphabet and numbers only only in c# 
Csharp :: how to close a form c# 
Csharp :: c# get gridview DataKeyNames 
Csharp :: c# int 
Csharp :: mvc session key exists 
Csharp :: how to get rid of the slashes in datetime variables c# 
Csharp :: vb.net remove last comma from string 
Csharp :: C# round number of digits after decimal point 
Csharp :: how to insert value to identity column using entity framwork 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =