Re: Fixe Point Decimal Data type
| From: | LinuxManMikeC | Date: | Thu, 17 Dec 2009 16:10:22 +0000 |
| Subject: | Re: Fixe Point Decimal Data type | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-300576@lists.php.net to get a copy of this message | ||
On Thu, Dec 17, 2009 at 7:21 AM, Raymond Irving <xwisdom@yahoo.com> wrote:
> Hello,
>
> Is there a way to represent numeric values as fixed point decimals in PHP? If not, why was this
> datatype not added?
>
> I think is very important to have fixed point decimals when working with money values. This
> would make it much easy than having to use the BCMath functions.
>
> If PHP had support for operator overloading then we could have easily implemented this datatype
> as a class:
>
> $a = new FixPoint(34.56, 2);
> $b = new FixPoint(34.55, 2);
>
> $c = $a-$b;
> echo ($c);
>
> $c+= 3;
> echo $c;
A lack of operator overloading wouldn't stop me if I really needed
such a datatype. Besides, I'd be willing to bet a class (or several)
have already been made.
There is also a problem with your example usage of FixPoint. By
initializing the FixPoint with a float you are still at risk for
rounding errors. It would be better to initialize a FixPoint from
integers or a string if accuracy is truly paramount.