Req #62538 [Com]: dec_ceil is wrong
| From: | anon at anon dot anon | Date: | Thu, 12 Jul 2012 09:18:50 +0000 |
| Subject: | Req #62538 [Com]: dec_ceil is wrong | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-171896@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62538&edit=1
ID: 62538
Comment by: anon at anon dot anon
Reported by: soneta dot kumar at gmail dot com
Summary: dec_ceil is wrong
Status: Not a bug
Type: Feature/Change Request
Package: *Math Functions
Operating System: All
PHP Version: 5.3.14
Block user comment: N
Private report: N
New Comment:
@Rasmus: The bug report appears to be about a function in a user comment on the ceil manual page: http://php.net/manual/en/function.ceil.php
Previous Comments:
------------------------------------------------------------------------
[2012-07-12 08:09:20] rasmus@php.net
So what is the bug in PHP? I see the bug in your code, but I don't see a PHP bug
here.
Step-by-step, exploding 1.019 on '.':
$numberpart=explode($separator,$number);
gives you: [0] => 1 [1] => 019
Now we take 019 and insert the separator '.' and position precision (2):
$numberpart[1]=substr_replace($numberpart[1],$separator,$precision,0);
Now we have: [0] => 1 [1] => 01.9
And since $numberpart[0]>0 we do the ceil line:
$numberpart[1]=ceil($numberpart[1]);
ceil(01.9) is obviously going to be 2 which is where you went wrong. Since
ceil() takes a float it is going to convert the string 01.9 to 1.9.
so we now have: [0] => 1 [1] => 2
This line seems redundant:
$ceil_number= array($numberpart[0],$numberpart[1]);
You are taking array elements and putting them into an array. Why not just use
the original array, but regardless, you $ceil_number array will have 1,2 in it.
return implode($separator,$ceil_number);
and this implode will return "1.2" as it should.
Why are you not just using number_format() here?
number_format(1.019,2) gives you what you want and it optionally takes decimal
and thousands separators too.
------------------------------------------------------------------------
[2012-07-12 07:21:42] soneta dot kumar at gmail dot com
Description:
------------
function ceil_dec($number,$precision,$separator)
{
$numberpart=explode($separator,$number);
$numberpart[1]=substr_replace($numberpart[1],$separator,$precision,0);
if($numberpart[0]>=0)
{$numberpart[1]=ceil($numberpart[1]);}
else
{$numberpart[1]=floor($numberpart[1]);}
$ceil_number= array($numberpart[0],$numberpart[1]);
return implode($separator,$ceil_number);
}
echo ceil_dec(1.019,2,".");
// Actual output = 1.2
// Expected output = 1.02
Test script:
---------------
This is solution :
function ceil_dec($number,$precision,$separator='.')
{
$appendZero = false;
$numberpart=explode($separator,$number);
$numberpart[1]=substr_replace($numberpart[1],$separator,$precision,0);
if($numberpart[1][0] == 0) { $appendZero = true;}
if($numberpart[0]>=0)
{$numberpart[1]=ceil($numberpart[1]);}
else
{$numberpart[1]=floor($numberpart[1]);}
if($appendZero){ $numberpart[1] = (int) '0'.$numberpart[1];}
$ceil_number= array($numberpart[0],$numberpart[1]);
return implode($separator,$ceil_number);
}
Expected result:
----------------
// Expected output = 1.02
Actual result:
--------------
// Expected output = 1.2
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62538&edit=1