Hi, Friday, December 6, 2002, 5:00:07 AM, you wrote: S> Hello again,
S> This is another PHP mathematical question. How can I display a bar over a S> number (overline) if it's a repeating decimal? When the user types in 1 by S> 3, they get 0.333333333333. I want it to display as 0.3 with the 3 S> overlined. How can I do this keeping in mind that not all numbers will be S> repeating decimals? Did this as an exercise :) function check_to_end($str,$pat){ $lp = strlen($pat); $ls = strlen($str); $x = 0; while(true){ $ls = strlen($str); if($ls > $lp){//string bigger than pattern $t = substr($str,0,$lp); if($t != $pat){ return false; } $str = substr($str,$lp); }else{//pattern too big .. checks tail end $pat = substr($pat,0,strlen($str)); if( $pat != $str) return false; //no repeat if($x < 2) return false; //didn't repeat enough return true; //found a pattern } $x++; } } function repeat($num){ $s = substr(number_format($num,16),0,-1); //make a string echo $s; list($a,$b) = split('\.',$s); //split decimal bit out $l = strlen($b); for($i=0;$i<$l;$i++){ //loop through string $o = 0; $k = $b[$i]; //number to find for($y = $i+1;$y<$l;$y++){ //loop look for same number $c = ord($b[$y]); if(ord($k) == ord($b[$y])){ //got a match $pat = substr($b,$i,$y-$i); //cut out pattern $str = substr($b,$i); //string to check if(check_to_end($str,$pat)){ //see if pattern runs till the end $o = 1; //yep if(ord($pat) == ord('0')) $o = 2; //were they just '0's break; } } } if($o)break; // all done } if($o == 1){ // a pattern $r = $a.'.'.substr($b,0,$i).'<span style="text-decoration:overline">'.$pat.'</span>'; }elseif($o == 2){ // whole bunch of ending 0s $r = (substr($b,0,$i) != '')? $a.'.'.substr($b,0,$i):$a; }else{ //no repeat $r = $s; } return $r; } //usage for($x = 1;$x < 100;$x++){ $a = 11/$x; $res = repeat($a); echo ' returned: '.$res.'<br>'; } The check_to_end() function could probably be replaced with a regex but those go in the same basket as vi with my old brain :) Regards Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php