Converting integers to roman numerals

前端 未结 29 2402
走了就别回头了
走了就别回头了 2020-12-02 09:16

I\'m trying to write a function that converts numbers to roman numerals. This is my code so far; however, it only works with numbers that are less than 400. Is there a quick

29条回答
  •  [愿得一人]
    2020-12-02 09:32

    In 1 line, not very efficient but works:

    public string RomanNumeralFrom(int number)
    {
        return
            new string('I', number)
                .Replace(new string('I', 1000), "M")
                .Replace(new string('I', 900), "CM")
                .Replace(new string('I', 500), "D")
                .Replace(new string('I', 400), "CD")
                .Replace(new string('I', 100), "C")
                .Replace(new string('I', 90), "XC")
                .Replace(new string('I', 50), "L")
                .Replace(new string('I', 40), "XL")
                .Replace(new string('I', 10), "X")
                .Replace(new string('I', 9), "IX")
                .Replace(new string('I', 5), "V")
                .Replace(new string('I', 4), "IV");
    }
    

提交回复
热议问题