Matthew McKay wrote:
Kim Madsen wrote:

Andre Dubuc wrote on 2010-01-02 02:20:

I need to extract the first digit after the decimal point from a number such as 28.56018, which should be '5'.

Since no one came up with the simple solution:

$num = "28.56018";
ereg("^[0-9]+\.([0-9]){1}", trim($num), $regs);
  $digit = $regs[1];
  print "no digit found";

My submission for a "simple" solution. I wish I had xslt2 =(
<xsl:template name="firstDigit">
   <xsl:param name="number"/>
<xsl:value-of select="number(substring(substring-after(number(translate(normalize-space($number),translate(normalize-space($number),'.0123456789',''),'')),'.'),1,1))"/>

nice to see a bit of xslt w/ functions :) just for the hell of it here's one off the top of my head

$num = 28.56018;
substr((int)($num*10) , -1); #5

and one without any functions..

(int)(($num-(int)$num)*10); #5

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to