note 84927 added to function.base-convert

From: Date: Wed, 06 Aug 2008 11:38:44 +0000
Subject: note 84927 added to function.base-convert
Groups: php.notes 
Request: Send a blank email to php-notes+get-143206@lists.php.net to get a copy of this message
In response to "Ray Paseur sometimes uses GMail" There is part of my class for specified operations on string etc. I've written two methods to convert numbers between roman and arabic notation: <?php class Text { public function Arab2Roman($number) { $out = ''; $translate = array( array('arab' => 5000, 'roman' => ''), array('arab' => 1000, 'roman' => 'M'), array('arab' => 900, 'roman' => 'CM'), array('arab' => 500, 'roman' => 'D'), array('arab' => 400, 'roman' => 'CD'), array('arab' => 100, 'roman' => 'C'), array('arab' => 90, 'roman' => 'XC'), array('arab' => 50, 'roman' => 'L'), array('arab' => 40, 'roman' => 'XL'), array('arab' => 10, 'roman' => 'X'), array('arab' => 9, 'roman' => 'IX'), array('arab' => 5, 'roman' => 'V'), array('arab' => 4, 'roman' => 'IV'), array('arab' => 1, 'roman' => 'I') ); if($number > $translate[0]['arab']) $out = false; else { $j = count($translate)-1; for($i=0;$i<$j;$i++) { if($number < $translate[$i]['arab'] && $number >= $translate[$i+1]['arab']) { $out .= $translate[$i+1]['roman']; $number -= $translate[$i+1]['arab']; if($number) $out .= Text::Arab2Roman($number); // do it recursive until $numer is not 0 break; } } } return ($out); } public function Roman2Arab($roman) { $out = 0; $translate = array( 'M' => 1000, 'D' => 500, 'C' => 100, 'L' => 50, 'X' => 10, 'V' => 5, 'I' => 1 ); $j = strlen($roman); for($i=0; $i<$j; $i++) { $out += $translate[$roman[$i]]; // find wrong order of roman chars if($i < $j-1 && $translate[$roman[$i]] < $translate[$roman[$i+1]]) { $out -= 2*$translate[$roman[$i]]; } } return ($out); } } ?> ---- Server IP: 82.177.164.100 Probable Submitter: 79.163.3.183 ---- Manual Page -- http://www.php.net/manual/en/function.base-convert.php Edit -- https://master.php.net/note/edit/84927 Del: integrated -- https://master.php.net/note/delete/84927/integrated Del: useless -- https://master.php.net/note/delete/84927/useless Del: bad code -- https://master.php.net/note/delete/84927/bad+code Del: spam -- https://master.php.net/note/delete/84927/spam Del: non-english -- https://master.php.net/note/delete/84927/non-english Del: in docs -- https://master.php.net/note/delete/84927/in+docs Del: other reasons-- https://master.php.net/note/delete/84927 Reject -- https://master.php.net/note/reject/84927 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#143206) next »