# kotlin实现罗马数字和阿拉伯数字转换

1.罗马数字转阿拉伯数字

2.阿拉伯数字转罗马数字

mapOf(1000 to "M",900 to "CM",500 to "D",400 to "CD",100 to "C",90 to "XC",50 to "L",40 to "XL",10 to "X",9 to "IX",5 to "V",4 to "IV",1 to "I")

val romeChar=mapOf(
'I' to 1,
'V' to 5,
'X' to 10,
'L' to 50,
'C' to 100,
'D' to 500,
'M' to 1000
)

class RomeNumber private constructor(){
constructor(number:String):this(){
this.number=number
}
var number:String=""
set(value){
if(value.toCharList().any{!romeChar.containsKey(it)}) throw object :Exception("It is not a legal Rome Number"){ } else \$number=value.toUpperCase()
}
public fun toArabicNumber():Int{
var lastNumber=0
var sum=0
number.toCharList().forEach {
var value:Int?=romeChar.get(it)
if (value!!>lastNumber) {sum-=lastNumber*2;sum+=value;}else sum+=value
lastNumber=value
}
return sum
}

override fun toString(): String {
return number
}
}

val additionMap = mapOf(1000 to "M",900 to "CM",500 to "D",400 to "CD",100 to "C",90 to "XC",50 to "L",40 to "XL",10 to "X",9 to "IX",5 to "V",4 to "IV",1 to "I")
val sb=StringBuilder()
var temp=this
while (temp != 0) {
while (temp >= it.getKey()) {
sb.append(it.getValue())
temp -= it.getKey()
}
}
}
}

#### 罗马数字与阿拉伯数字的相互转化

2017-10-16 14:01:32

#### 罗马数字转换阿拉伯数字（Java版，考虑较为全面）

2009-09-01 20:23:00

#### 送给大家一个把阿拉伯数字与罗马数字互换的代码 -.- 仅支持4000以下的转化

2016-06-04 08:55:20

#### 罗马数字和阿拉伯数字互化<java实现>

2016-11-21 22:30:23

#### 阿拉伯数字(1到4000)转化为罗马数字的javascript实现

2017-07-18 16:14:58

#### Java经典题：罗马数字和阿拉伯数字的相互转换

2015-05-17 18:50:36

#### 【C++】将罗马数字转换成阿拉伯数字

2017-10-29 14:22:00

#### java将阿拉伯数字转化为罗马数字 I II III IV V VI

2015-06-15 16:26:15

#### python实现阿拉伯数字和罗马数字的互相转换

2017-08-05 16:43:21

#### 罗马数字转换为阿拉伯数字

2017-02-03 15:26:56