|
View:
New views
18 Messages
—
Rating Filter:
Alert me
|
|
|
Need unrounded precisionHi,
I need to extract the first digit after the decimal point from a number such as 28.56018, which should be '5'. I've tried a few methods to accomplish this. If I use 'ini_set' I would need to know the number of digits before the decimal (which, unfortunately, I would not have access to). Then I've tried: <?php $elapsed = 28.56018; $digit = round($elapsed, 1); // rounds result is '6' $digit = number_format($elapsed, 1); // still rounds result to '6' ?> What I need is only the first digit after the decimal -- all the rest could be 'chopped' or discarded but without rounding the first digit after the decimal point. Is there any way of doing this? I'm stumped. Tia, Andre -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
Re: Need unrounded precisionOn Fri, Jan 1, 2010 at 9:20 PM, Andre Dubuc <aajdubuc@...> wrote:
> Hi, > > I need to extract the first digit after the decimal point from a number such > as 28.56018, which should be '5'. > > I've tried a few methods to accomplish this. If I use 'ini_set' I would need > to know the number of digits before the decimal (which, unfortunately, I > would not have access to). > > Then I've tried: > > <?php > > $elapsed = 28.56018; > > $digit = round($elapsed, 1); // rounds result is '6' > $digit = number_format($elapsed, 1); // still rounds result to '6' > > ?> > > What I need is only the first digit after the decimal -- all the rest could > be 'chopped' or discarded but without rounding the first digit after the > decimal point. > > Is there any way of doing this? > > I'm stumped. > > Tia, > Andre > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > Try sprintf with a format of %0.1f. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
Re: Need unrounded precision [RESOLVED]Thanks Eddie,
Actually while waiting for some re[lies, I resolved the problem. I realized that '$elapsed' will always have only two digits before the decimal point. Thus I was able to use 'number_format($elapsed, 2);' and to give me the desired result. Thanks for the quick reply! Andre On October 11, 2009 08:50:11 pm Eddie Drapkin wrote: > On Fri, Jan 1, 2010 at 9:20 PM, Andre Dubuc <aajdubuc@...> wrote: > > Hi, > > > > I need to extract the first digit after the decimal point from a number > > such as 28.56018, which should be '5'. > > > > I've tried a few methods to accomplish this. If I use 'ini_set' I would > > need to know the number of digits before the decimal (which, > > unfortunately, I would not have access to). > > > > Then I've tried: > > > > <?php > > > > $elapsed = 28.56018; > > > > $digit = round($elapsed, 1); // rounds result is '6' > > $digit = number_format($elapsed, 1); // still rounds result to '6' > > > > ?> > > > > What I need is only the first digit after the decimal -- all the rest > > could be 'chopped' or discarded but without rounding the first digit > > after the decimal point. > > > > Is there any way of doing this? > > > > I'm stumped. > > > > Tia, > > Andre > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > Try sprintf with a format of %0.1f. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
Re: Need unrounded precisionAndre Dubuc wrote:
> Hi, > > I need to extract the first digit after the decimal point from a number such > as 28.56018, which should be '5'. > > I've tried a few methods to accomplish this. If I use 'ini_set' I would need > to know the number of digits before the decimal (which, unfortunately, I > would not have access to). > > Then I've tried: > > <?php > > $elapsed = 28.56018; > > $digit = round($elapsed, 1); // rounds result is '6' > $digit = number_format($elapsed, 1); // still rounds result to '6' > > ?> > > What I need is only the first digit after the decimal -- all the rest could > be 'chopped' or discarded but without rounding the first digit after the > decimal point. > > Is there any way of doing this? > > I'm stumped. > > Tia, > Andre > FYI: did you know that your computer thinks it is in the future? You need to check your date settings... -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
Re: Need unrounded precisionYup! I had noticed that I had forgot to reset to today's date after working on
the code, just after I sent the messages. Thanks. Andre On October 11, 2009 09:14:56 pm Jim Lucas wrote: > Andre Dubuc wrote: > > Hi, > > > > I need to extract the first digit after the decimal point from a number > > such as 28.56018, which should be '5'. > > > > I've tried a few methods to accomplish this. If I use 'ini_set' I would > > need to know the number of digits before the decimal (which, > > unfortunately, I would not have access to). > > > > Then I've tried: > > > > <?php > > > > $elapsed = 28.56018; > > > > $digit = round($elapsed, 1); // rounds result is '6' > > $digit = number_format($elapsed, 1); // still rounds result to '6' > > > > ?> > > > > What I need is only the first digit after the decimal -- all the rest > > could be 'chopped' or discarded but without rounding the first digit > > after the decimal point. > > > > Is there any way of doing this? > > > > I'm stumped. > > > > Tia, > > Andre > > FYI: did you know that your computer thinks it is in the future? > > You need to check your date settings... > > -- > Jim Lucas > > "Some men are born to greatness, some achieve greatness, > and some have greatness thrust upon them." > > Twelfth Night, Act II, Scene V > by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
RE: Need unrounded precision-----Original Message-----
From: Andre Dubuc [mailto:aajdubuc@...] Sent: 02 January 2010 03:20 AM To: php-general@... Subject: [PHP] Need unrounded precision Hi, I need to extract the first digit after the decimal point from a number such as 28.56018, which should be '5'. I've tried a few methods to accomplish this. If I use 'ini_set' I would need to know the number of digits before the decimal (which, unfortunately, I would not have access to). Then I've tried: <?php $elapsed = 28.56018; $digit = round($elapsed, 1); // rounds result is '6' $digit = number_format($elapsed, 1); // still rounds result to '6' ?> What I need is only the first digit after the decimal -- all the rest could be 'chopped' or discarded but without rounding the first digit after the decimal point. Is there any way of doing this? I'm stumped. Tia, Andre -- One way that should work regardless the number of digits before/after the decimal is: - convert to string (sprintf or typecast) - strpos the decimal - grab the char from the next position Cheers Arno -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
RE: Need unrounded precisionMay be this will work
$elapsed = 28.56018; $elapsed_rel = (int) 28.56018; $elapsed_deci = $elapsed - $elapsed_rel; $deci = ((int) ($elapsed_deci * 10))/10; $final = $elapsed_rel + $deci; With regards, Chetan Dattaram Rane | Software Engineer | Persistent Systems chetan_rane@... | Cell: +91 9766646714 | Tel: +91 (0832) 30 79228 Innovation in software product design, development and delivery- www.persistentsys.com -----Original Message----- From: Arno Kuhl [mailto:akuhl@...] Sent: Monday, October 12, 2009 12:07 PM To: 'Andre Dubuc'; php-general@... Subject: RE: [PHP] Need unrounded precision -----Original Message----- From: Andre Dubuc [mailto:aajdubuc@...] Sent: 02 January 2010 03:20 AM To: php-general@... Subject: [PHP] Need unrounded precision Hi, I need to extract the first digit after the decimal point from a number such as 28.56018, which should be '5'. I've tried a few methods to accomplish this. If I use 'ini_set' I would need to know the number of digits before the decimal (which, unfortunately, I would not have access to). Then I've tried: <?php $elapsed = 28.56018; $digit = round($elapsed, 1); // rounds result is '6' $digit = number_format($elapsed, 1); // still rounds result to '6' ?> What I need is only the first digit after the decimal -- all the rest could be 'chopped' or discarded but without rounding the first digit after the decimal point. Is there any way of doing this? I'm stumped. Tia, Andre -- One way that should work regardless the number of digits before/after the decimal is: - convert to string (sprintf or typecast) - strpos the decimal - grab the char from the next position Cheers Arno -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Ltd. does not accept any liability for virus infected mails. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
Re: Need unrounded precisionA simple way to do that would be:
$elapsed = strval( 28.56018 ); $pos = strpos( $elapsed, '.' ); echo $elapsed[ ++$pos ]; On Sat, Jan 2, 2010 at 2:20 AM, Andre Dubuc <aajdubuc@...> wrote: > Hi, > > I need to extract the first digit after the decimal point from a number > such > as 28.56018, which should be '5'. > > I've tried a few methods to accomplish this. If I use 'ini_set' I would > need > to know the number of digits before the decimal (which, unfortunately, I > would not have access to). > > Then I've tried: > > <?php > > $elapsed = 28.56018; > > $digit = round($elapsed, 1); // rounds result is '6' > $digit = number_format($elapsed, 1); // still rounds result to '6' > > ?> > > What I need is only the first digit after the decimal -- all the rest could > be 'chopped' or discarded but without rounding the first digit after the > decimal point. > > Is there any way of doing this? > > I'm stumped. > > Tia, > Andre > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Thanks, Diogo Neves Web Developer @ SAPO.pt by PrimeIT.pt |
|
|
RE: Need unrounded precision> -----Original Message-----
> From: Diogo Neves [mailto:dafneves@...] > Sent: Monday, October 12, 2009 9:19 AM > To: Andre Dubuc > Cc: php-general@... > Subject: Re: [PHP] Need unrounded precision > > A simple way to do that would be: > > $elapsed = strval( 28.56018 ); > $pos = strpos( $elapsed, '.' ); > echo $elapsed[ ++$pos ]; > > On Sat, Jan 2, 2010 at 2:20 AM, Andre Dubuc <aajdubuc@...> > wrote: > > > Hi, > > > > I need to extract the first digit after the decimal point from a > number > > such > > as 28.56018, which should be '5'. Couldn't this be done with just simple math functions? $a = 28.56018; $b = intval(($a*10)-(intval($a)*10)); or: $a = 28.56018; $b = intval(($a-intval($a))*10); Jaime -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
RE: Need unrounded precision> Couldn't this be done with just simple math functions? indeed: $a = 28.56018; $b = $a * 10 % 10 >> 0; Regards _________________________________________________________________ Windows Live Hotmail: Your friends can get your Facebook updates, right from Hotmail®. http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_4:092009 |
|
|
RE: Need unrounded precision>> Couldn't this be done with just simple math functions?
> >indeed: > >$a = 28.56018; >$b = $a * 10 % 10 >> 0; Hmmm... Didn't think about this, but % only works with int values, so $b = $a * 10 % 10; Should work as well. Jaime -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
RE: Need unrounded precision> Hmmm... Didn't think about this, but % only works with int values it was just future prof precaution since this statement is false for many other languages. In few words I am not sure PHP6 does the same ... never mind so far Regards _________________________________________________________________ Keep your friends updated—even when you’re not signed in. http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_5:092010 |
|
|
RE: Need unrounded precision>> Hmmm... Didn't think about this, but % only works with int values
> >it was just future prof precaution since this statement is false for many other languages. >In few words I am not sure PHP6 does the same ... never mind so far Good to know. In that case, I would probably just use intval() instead of >> since it's clearer and bitwise shifts aren't necessarily integer only either. Jaime -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
RE: Need unrounded precisionbitwise right shift is probably the fastest cast to int so far ... still in many languages, intval is a function call being a cast in both cases (int) is good as well ... bitwise, casting, works with strings, arrays, boolean, whatever as well. I don't think there is any difference in php, except when the integer is "too big" ... but this was not the case, we had to deal with 1 to 10 :-) Regards > From: jbozza@... > To: an_red@... > CC: php-general@... > Date: Mon, 12 Oct 2009 11:33:10 -0500 > Subject: RE: [PHP] Need unrounded precision > > >> Hmmm... Didn't think about this, but % only works with int values > > > >it was just future prof precaution since this statement is false for many other languages. > >In few words I am not sure PHP6 does the same ... never mind so far > > Good to know. In that case, I would probably just use intval() instead of >> since it's clearer and bitwise shifts aren't necessarily integer only either. > > Jaime > _________________________________________________________________ Windows Live: Make it easier for your friends to see what you’re up to on Facebook. http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_2:092009 |
|
|
Re: Need unrounded precisionOn Oct 12, 2009, at 2:37 PM, Andrea Giammarchi wrote:
> bitwise right shift is probably the fastest cast to int so far ... > still in many languages, intval is a function call > > being a cast in both cases (int) is good as well ... bitwise, > casting, works with strings, arrays, boolean, whatever as well. > > I don't think there is any difference in php, except when the > integer is "too big" ... but this was not the case, we had to deal > with 1 to 10 :-) > > Regards > >> From: jbozza@... >> To: an_red@... >> CC: php-general@... >> Date: Mon, 12 Oct 2009 11:33:10 -0500 >> Subject: RE: [PHP] Need unrounded precision >> >>>> Hmmm... Didn't think about this, but % only works with int values >>> >>> it was just future prof precaution since this statement is false >>> for many other languages. >>> In few words I am not sure PHP6 does the same ... never mind so far >> >> Good to know. In that case, I would probably just use intval() >> instead of >> since it's clearer and bitwise shifts aren't >> necessarily integer only either. >> >> Jaime I like the bitwise shifting, but here's another potential (albeit probably slower) solution. $a = 28.56018; list (,$dec) = explode ('.', $a); $b = $dec{0}; Cheers, ~Philip -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
Re: Need unrounded precisionHello
Andre Dubuc wrote on 2010-01-02 02:20: > Hi, > > 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); if($regs[1]) $digit = $regs[1]; else print "no digit found"; -- Kind regards Kim Emax -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
Re: Need unrounded precisionKim Madsen wrote:
> Hello > > Andre Dubuc wrote on 2010-01-02 02:20: >> Hi, >> >> 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); > if($regs[1]) > $digit = $regs[1]; > else > print "no digit found"; > <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))"/> </xsl:template> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
Re: Need unrounded precisionMatthew McKay wrote:
> Kim Madsen wrote: >> Hello >> >> Andre Dubuc wrote on 2010-01-02 02:20: >>> Hi, >>> >>> 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); >> if($regs[1]) >> $digit = $regs[1]; >> else >> 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))"/> > > </xsl:template> > 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 (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
| Free embeddable forum powered by Nabble | Forum Help |