| Convertion of integer to roman numbers |
|
|
| Contributed by Pierre Charbonneau P.Eng. | |
| 06 April 2007 | |
|
In publication and display, we often want to put automatically the current year in roman numbers but how ? Here's a convertion function that converts integer values between 1 and 3999 to the roman numbers. You may use it for many purpose like pages footers and headers where the current year is written as a Copyright for documents or web sites. The best place to put code is in a library function that you call when needed. method intToRoman(nIntegerSI SmallInt) String ; By Pierre Charbonneau P. Eng. ; Saguenay, Québec, Canada var nSymbSI, iSI, jSI, wk0SI, wk1SI, wk2SI SmallInt nRomanST String romanSymbDAST DynArray[] String nWeightDASI DynArray[] SmallInt concatenateDAST DynArray[] AnyType flag1L Logical endVar nSymbSI = 7 ; Number Weight nWeightDASI[1] = 1 nWeightDASI[2] = 5 nWeightDASI[3] = 10 nWeightDASI[4] = 50 nWeightDASI[5] = 100 nWeightDASI[6] = 500 nWeightDASI[7] = 1000 ; Symbol Set romanSymbDAST[nWeightDASI[1]] = "I" romanSymbDAST[nWeightDASI[2]] = "V" romanSymbDAST[nWeightDASI[3]] = "X" romanSymbDAST[nWeightDASI[4]] = "L" romanSymbDAST[nWeightDASI[5]] = "C" romanSymbDAST[nWeightDASI[6]] = "D" romanSymbDAST[nWeightDASI[7]] = "M" wk0SI = nIntegerSI wk1SI = 0 wk2SI = 0 flag1L = false for jSI from nSymbSI to 0 step -1 concatenateDAST[jSI] = blank() endFor nRomanST.blank() ; Convertion for iSI from nSymbSI to 1 step -2 ; Integer-Mantissa separation wk1SI = int(wk0SI/nWeightDASI[iSI]) ; ex: int(X140/1000) = X wk2SI = wk0SI.mod(int(nWeightDASI[iSI])) ; ex: X140.mod(1000) = 140 if wk1SI >= 1 and wk1SI < 4 then ; Value higher than 1 x power of 10 flag1L = true for jSI from 1 to wk1SI step 1 concatenateDAST[iSI] = concatenateDAST[iSI] + romanSymbDAST[nWeightDASI[iSI]] endFor wk0SI = wk2SI ; Keep rest for next iteration else if iSI < nSymbSI then if wk1SI = 4 then ; Value higher to 3 x power of 10, i < 7 blocks input value of 4000 and over flag1L = true concatenateDAST[iSI] = concatenateDAST[iSI] + romanSymbDAST[nWeightDASI[iSI]] + romanSymbDAST[nWeightDASI[iSI+1]] wk0SI = wk2SI ; Keep rest for next iteration else if wk1SI = 5 then ; Value higher to 5 x power of 10, i < 7 blocks input value of 4000 and over flag1L = true concatenateDAST[iSI] = concatenateDAST[iSI] + romanSymbDAST[nWeightDASI[iSI+1]] wk0SI = wk2SI ; Keep rest for next iteration else if wk1SI >= 6 and wk1SI < 9 then ; Value higher to 6 to 8 x power of 10, i < 7 blocks input value of 4000 and over concatenateDAST[iSI] = concatenateDAST[iSI] + romanSymbDAST[nWeightDASI[iSI+1]] for jSI from 6 to wk1SI concatenateDAST[iSI] = concatenateDAST[iSI] + romanSymbDAST[nWeightDASI[iSI]] endFor wk0SI = wk2SI ; Keep rest for next iteration else if wk1SI = 9 then ; Here it's 9 flag1L = true concatenateDAST[iSI] = concatenateDAST[iSI] + romanSymbDAST[nWeightDASI[iSI]] concatenateDAST[iSI] = concatenateDAST[iSI] + romanSymbDAST[nWeightDASI[iSI+2]] wk0SI = wk2SI ; Keep rest for next iteration endIf endIf endIf endIf endIf endIf endFor for iSI from nSymbSI to 1 step -1 nRomanST = nRomanST + concatenateDAST[iSI] endFor if not flag1L then nRomanST = "Value out of range" endIf return nRomanST endMethod |
|
| Last Updated ( 29 October 2008 ) |
| < Prev | Next > |
|---|





