Roman numerals/Decode: Difference between revisions

added MATLAB example
m (→‎{{header|Sidef}}: modified the code to work with the latest version of Sidef)
(added MATLAB example)
Line 2,391:
<lang Mathematica>FromDigits["MMCDV", "Roman"]</lang>
returns 2405
 
=={{header|MATLAB}}==
<lang Matlab>function x = rom2dec(s)
% ROM2DEC converts Roman numbers to decimal
 
% store Roman digits values: I=1, V=5, X=10, L=50, C=100, D=500, M=1000
digitsValues = [0 0 100 500 0 0 0 0 1 0 0 50 1000 0 0 0 0 0 0 0 0 5 0 10 0 0];
% convert Roman number to array of values
values = digitsValues(s-'A'+1);
% change sign if next value is bigger
x = sum(values .* [sign(diff(-values)+eps),1]);
 
end</lang>
Here is a test:
<lang Matlab>romanNumbers = {'MMMCMXCIX', 'XLVIII', 'MMVIII'};
for n = 1 : numel(romanNumbers)
fprintf('%10s = %4d\n',romanNumbers{n}, rom2dec(romanNumbers{n}));
end</lang>
{{out}}
<pre>
MMMCMXCIX = 3999
XLVIII = 48
MMVIII = 2008
</pre>
 
 
=={{header|Mercury}}==
Anonymous user