Skip to content

Instantly share code, notes, and snippets.

@azproduction
Forked from 140bytes/LICENSE.txt
Created January 24, 2012 07:21
Show Gist options
  • Save azproduction/1668667 to your computer and use it in GitHub Desktop.
Save azproduction/1668667 to your computer and use it in GitHub Desktop.
Decimal to Roman

Decimal to Roman

Converts up to 3999 using basic language (IVXLCDM) or up to 39999 using extended (IVXLCDMↁↂ)

Demo http://jsfiddle.net/JSBFa/1/

(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;
})
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}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Mikhail Davydov <azazel.private@gmail.com>
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.
{
"name": "decimal2roman",
"description": "Decimal to roman converter",
"keywords": [
"decimal",
"number",
"roman"
]
}
<!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>
@atk
Copy link

atk commented Jan 25, 2012

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