1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How can convert a Number to Roman format?

Discussion in 'C#' started by alamlinks, Sep 3, 2012.

  1. #1
    How can convert a Number to Roman format in asp.net using C#?

    The answer is :


    static private string Roman(int i)
    {
    return Thounds(i);
    }
    static private string Thounds(int i)
    {
    string strNo = "";
    for (int x = 0; x < i / 1000; x++)
    {
    strNo =strNo+ "M";
    }
    strNo = strNo + UptoThousand(i % 1000);
    return strNo;
    }
    static private string UptoThousand(int i)
    {
    string strNo="";
    for (int x = 0; x < i/100; x++)
    {
    strNo = strNo + "C";
    }
    if (strNo.Length == 4)
    {
    strNo = "CD";
    }
    else if (strNo.Length == 5)
    {
    strNo = "D";
    }
    else if (strNo.Length == 9)
    {
    string temp = "CM";
    strNo = temp;
    }
    else if (strNo.Length >5 && strNo.Length <9)
    {
    string temp = strNo.Substring(0, 5);
    temp = "D";
    strNo = temp + strNo.Substring(5);
    }
    strNo = strNo + UptoHundreds(i%100);
    return strNo;
    }
    static private string UptoHundreds(int i)
    {
    string strNo = "";
    int k = i % 10;
    for (int x = 0; x < i / 10; x++)
    strNo = strNo + "X";
    if (strNo.Length == 4)
    {
    strNo = "IL";
    }
    else if (strNo.Length == 5)
    {
    strNo = "L";
    }
    else if (strNo.Length == 9)
    {
    string temp = "XC";
    strNo=temp ;
    }
    else if (strNo.Length > 5 && strNo.Length < 9)
    {
    string temp = strNo.Substring(0, 5);
    temp = "L";
    strNo = temp + strNo.Substring(5);
    }
    strNo = strNo + UpToTen(i % 10);
    return strNo;
    }
    static private string UpToTen(int i)
    {
    string strNo = "";
    for (int x = 1; x <= i; x++)
    {
    strNo = strNo + "I";
    }
    if (strNo.Length == 4)
    {
    strNo = "IV";
    }
    else if (strNo.Length == 5)
    {
    strNo = "V";
    }
    else if (strNo.Length == 9)
    {
    string temp = "IX";
    strNo = temp;
    }
    else if (strNo.Length > 5 && strNo.Length < 9)
    {
    string temp = strNo.Substring(0, 5);
    temp = "V";
    strNo = temp + strNo.Substring(5);
    }
    return strNo;
    }

    If any one to suggest me any thing OR any question regarding ASP.Net then Please welcome, mail me OR PM me OR reply to this thread.

    Regards
    Noor Alam
     
    Last edited by a moderator: Sep 3, 2012
    alamlinks, Sep 3, 2012 IP
  2. alamlinks

    alamlinks Well-Known Member

    Messages:
    992
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    140
    #2
    Thank you very much for this tutorial
     
    alamlinks, Sep 3, 2012 IP
  3. dwirch

    dwirch Well-Known Member

    Messages:
    239
    Likes Received:
    12
    Best Answers:
    1
    Trophy Points:
    135
    #3
    This should do it.

    'example usage:
    'response.write "2001: " & roman(2001)
    
    Function roman(number)
    
    	dim v, w, x, y
    	dim str1
    	dim roman_unit
    	dim roman_tens
    	dim roman_hund
    	dim roman_thou
    
    	roman_unit = Array("","I","II","III","IV","V","VI","VII","VIII","IX")
    	roman_tens = Array("","X","XX","XXX","XL","L","LX","LXX","LXXX","XC")
    	roman_hund = Array("","C","CC","CCC","CD","D","DC","DCC","DCCC","CM")
    	roman_thou = Array("","M","MM","MMM","MMMM","MMMMM")
    
    	v = ((number - (number mod 1000)) / 1000)
    	number = (number mod 1000)
    	w = ((number - (number mod 100)) / 100)
    	number = (number mod 100)
    	x = ((number - (number mod 10)) / 10)
    	y = (number mod 10)
    	roman = roman_thou(v) & roman_hund(w) & roman_tens(x) & roman_unit(y)
    
    End Function
    Code (markup):
     
    dwirch, Sep 8, 2012 IP