Game Maker Games, Articles, Tutorials & More

Game Maker Network


Howdy, Guest! Please sign in or register an account.

Print

arab2roman

By Daniel · January 11, 2009

Constructs a Roman numeric representation of a given integer (which should be typed as a real, not a string) between 1 and 3,999,999.

/* arab2roman(int)
 *
 * Constructs a Roman numeric representation of a given integer (which
 * should be typed as a real, not a string) between 1 and 3,999,999.
 */

var map, key, out;

map = ds_map_create();
ds_map_add(map, 1000000, 'm'); ds_map_add(map, 900000, 'cm');
ds_map_add(map, 500000,  'd'); ds_map_add(map, 400000, 'cd');
ds_map_add(map, 100000,  'c'); ds_map_add(map, 90000,  'xc');
ds_map_add(map, 50000,   'l'); ds_map_add(map, 40000,  'xl');
ds_map_add(map, 10000,   'x'); ds_map_add(map, 9000,   'Mx');
ds_map_add(map, 5000,    'v'); ds_map_add(map, 4000,   'Mv');
ds_map_add(map, 1000,    'M'); ds_map_add(map, 900,    'CM');
ds_map_add(map, 500,     'D'); ds_map_add(map, 400,    'CD');
ds_map_add(map, 100,     'C'); ds_map_add(map, 90,     'XC');
ds_map_add(map, 50,      'L'); ds_map_add(map, 40,     'XL');
ds_map_add(map, 10,      'X'); ds_map_add(map, 9,      'IX');
ds_map_add(map, 5,       'V'); ds_map_add(map, 4,      'IV');
ds_map_add(map, 1,       'I');

out = '';
for (key = ds_map_find_last(map); key > 0; key = ds_map_find_previous(map, key)) {
  while (argument0 >= key) {
    argument0 -= key;
    out += ds_map_find_value(map, key);
  }
}
ds_map_destroy(map);
return out;

Comments

There are no comments to display.

Post a Comment

You must be signed in to post comments.