Miscellaneous Code

  Home arrow Miscellaneous Code arrow roman2dec / dec2roman
MISCELLANEOUS CODE

roman2dec / dec2roman
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2003-10-08

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    Two functions to convert integers into a roman string and back.
    Don't know if anybody needs this, but it was fun to make.

    By : Dios

    <?php
    // Function that calculates the roman string to the given number:
    function dec2roman($f)
    {
    // Return false if either $f is not a real number, $f is bigger than 3999 or $f is lower or equal to 0:
    if(!is_numeric($f) || $f > 3999 || $f <= 0) return false;

    // Define the roman figures:
    $roman = array('M' => 1000, 'D' => 500, 'C' => 100, 'L' => 50, 'X' => 10, 'V' => 5, 'I' => 1);

    // Calculate the needed roman figures:
    foreach($roman as $k => $v) if(($amount[$k] = floor($f / $v)) > 0) $f -= $amount[$k] * $v;

    // Build the string:
    $return = '';
    foreach($amount as $k => $v)
    {
    $return .= $v <= 3 ? str_repeat($k, $v) : $k . $old_k;
    $old_k = $k;
    }

    // Replace some spacial cases and return the string:
    return str_replace(array('VIV','LXL','DCD'), array('IX','XC','CM'), $return);
    }



    // Function to get the decimal value of a roman string:
    function roman2dec($str = '')
    {
    // Return false if not at least one letter is in the string:
    if(is_numeric($str)) return false;

    // Define the roman figures:
    $roman = array('M' => 1000, 'D' => 500, 'C' => 100, 'L' => 50, 'X' => 10, 'V' => 5, 'I' => 1);

    // Convert the string to an array of roman values:
    for($i = 0; $i < strlen($str); $i++) if(isset($roman[strtoupper($str[$i])])) $values[] = $roman[strtoupper($str[$i])];

    // Calculate the sum of that array:
    $sum = 0;
    while($current = current($values))
    {
    $next = next($values);
    $next > $current ? $sum += $next - $current + 0 * next($values) : $sum += $current;
    }

    // Return the value:
    return $sum;
    }
    ?>
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Miscellaneous Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap