add Ox.formatRoman

This commit is contained in:
rolux 2012-06-20 11:57:22 +02:00
parent c575686fca
commit 14d57f70bb

View file

@ -631,6 +631,31 @@ Ox.formatPercent = function(number, total, decimals) {
return Ox.formatNumber(number / total * 100, decimals) + '%' return Ox.formatNumber(number / total * 100, decimals) + '%'
}; };
/*@
Ox.formatRoman <f> Formats a number as a roman numeral
> Ox.formatRoman(1999)
'MCMXCIX'
> Ox.formatRoman(2001)
'MMI'
> Ox.formatRoman(0)
''
> Ox.formatRoman(10000)
'MMMMMMMMMM'
@*/
Ox.formatRoman = function(number) {
var string = '';
Ox.forEach({
M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90,
L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1
}, function(value, roman) {
while (number >= value) {
string += roman;
number -= value;
}
});
return string;
};
/*@ /*@
Ox.formatString <f> Basic string formatting Ox.formatString <f> Basic string formatting
> Ox.formatString('{0}{1}', ['foo', 'bar']) > Ox.formatString('{0}{1}', ['foo', 'bar'])