Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 03-22-2008, 11:49 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Roman numbers

Well, gives the ability to convert numbers into roman numbers and vice versa:

PHP Code:
public function romanToNumber(roman) {
  
temp.table_l = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
  
temp.table_n = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
  
temp.0;
  for(
temp.0roman.length(); ++) {
    if (
roman.substring(i2in table_l) {
      
+= table_n[table_l.index(roman.substring(i2))];
      
++;
    }else 
+= table_n[table_l.index(roman.charat(i))];
  }
  return 
n;
}
public function 
numberToRoman(number) {
  
temp.table_n = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
  
temp.table_l = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
  
temp.out ""temp.number2 number;
  for(
temp.table_n) {
    while(
number2 >= n) {
      
number2 -= n;
      
out @= table_l[table_n.index(n)];
    }
  }
  return 
out;

(Credits to Ian )

Example:

PHP Code:
function onCreated() {
  echo(
numberToRoman(947));
  echo(
romanToNumber("CMXLVII"));
  
/*
  CMXLVII
  947
  */
  
echo(numberToRoman(2008));
  echo(
romanToNumber("MMVIII"));
  
/* 
  MMVIII
  2008
  */
  
echo(numberToRoman(romanToNumber("MMVIII")));
  
// MMVIII

anyways, enjoy?
__________________
Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 07:07 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.