RE: Need unrounded precision

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxxxx  | 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@xxxxxxxxxxxx] 
Sent: Monday, October 12, 2009 12:07 PM
To: 'Andre Dubuc'; php-general@xxxxxxxxxxxxx
Subject: RE:  Need unrounded precision

-----Original Message-----
From: Andre Dubuc [mailto:aajdubuc@xxxxxxxxxxx] 
Sent: 02 January 2010 03:20 AM
To: php-general@xxxxxxxxxxxxx
Subject:  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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux