Converts up to 3999 using basic language (IVXLCDM) or up to 39999 using extended (IVXLCDMↁↂ)
-
-
Save azproduction/1668667 to your computer and use it in GitHub Desktop.
Decimal to Roman
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ( | |
a, // Decimal number | |
m, // Reverse replacement rules | |
// 0 1 2 3 4 5 6 7 8 9 | |
// default: ["", "0", "00", "000", "10", "1", "01", "001", "0001", "20"] | |
// | |
// example: | |
// 0 -> '' | |
// 1 -> I 0 - I | |
// 7 -> VII 1 - V, 0 - I | |
// 9 -> IX 2 - X, 0 - I | |
// 11(1 + 1 * 10) -> XI | |
l, // Language | |
// default: "IVXLCDM" (1,5,10,50,100,500,1000) | |
// extended: "IVXLCDMↁↂ" (..., 5000, 10000) | |
i,j,r,z) { | |
for ( | |
a=(a+'').split('').reverse(), // reverse decimal number and make it iterable | |
i=-1, // decimal number digit pointer | |
r='' // result | |
; | |
a[++i] // while digits do... | |
;) | |
for ( | |
j=-1 // reset replacement pointer | |
; | |
z=m[a[i]][++j] // get next replacement by current digit and replacement | |
// z = replacement_rules_by_decimal[decimal_number][replacement_index] | |
;) | |
r = l[+z+i*2] + r; // replace: language[current_replcement + decimal_digit_index * 2] and save | |
return r; | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(a,b,c,d,e,f,g){for(a=(a+"").split(f="").reverse(d=-1);a[++d];)for(e=0;g=b[a[d]][e++];)f=c[+g+d*2]+f;return f} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Mikhail Davydov <[email protected]> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "decimal2roman", | |
"description": "Decimal to roman converter", | |
"keywords": [ | |
"decimal", | |
"number", | |
"roman" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<title>Decimal to roman</title> | |
<div>Expected value: <b>MCMLIV</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
var decimal2roman = function(a,b,c,d,e,f,g){for(a=(a+"").split(f="").reverse(d=-1);a[++d];)for(e=0;g=b[a[d]][e++];)f=c[+g+d*2]+f;return f}; | |
var MAP_RULES = "90900900091091901900190001920".split(9); // default rule: up to 3 same digits in row | |
var LANGUAGE = "IVXLCDM" || "IVXLCDMↁↂ"; // default or extended language | |
document.getElementById("ret").innerHTML = decimal2roman(1954, MAP_RULES, LANGUAGE); | |
</script> |
Save more bytes.
function(a,b,c,d,e,f,g){for(d in a=(a+"").split(f="").reverse())for(e=0;g=b[a[d]][e++];)f=c[+g+d*2]+f;return f}
Why not use f+=...
instead of f=...+f
? Ah, because it needs to be the other way round.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Save some bytes.
function(a,b,c,d,e,f,g){for(a=(a+"").split(f="").reverse(d=-1);a[++d];)for(e=0;g=b[a[d]][e++];)f=c[+g+d*2]+f;return f}