RE: [PHP] Need unrounded precision
| From: | Chetan Rane | Date: | Mon, 12 Oct 2009 07:03:10 +0000 |
| Subject: | RE: [PHP] Need unrounded precision | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-298851@lists.php.net to get a copy of this message | ||
May 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@persistent.co.in | 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@telkomsa.net]
Sent: Monday, October 12, 2009 12:07 PM
To: 'Andre Dubuc'; php-general@lists.php.net
Subject: RE: [PHP] Need unrounded precision
-----Original Message-----
From: Andre Dubuc [mailto:aajdubuc@webhart.net]
Sent: 02 January 2010 03:20 AM
To: php-general@lists.php.net
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.