Fixe Point Decimal Data type
Fixe Point Decimal Data type
am 17.12.2009 15:21:34 von Raymond Irving
--0-1441322339-1261059694=:42856
Content-Type: text/plain; charset=us-ascii
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;
--0-1441322339-1261059694=:42856--
Re: Fixe Point Decimal Data type
am 17.12.2009 15:49:18 von Floyd Resler
I may be over-simplifying or not completely understanding fixed point =
decimals, but would number_format work?
Take care,
Floyd
On Dec 17, 2009, at 9:21 AM, Raymond Irving wrote:
> Hello,
>=20
> Is there a way to represent numeric values as fixed point decimals in =
PHP? If not, why was this datatype not added?
>=20
> 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.=20
>=20
> If PHP had support for operator overloading then we could have easily =
implemented this datatype as a class:
>=20
> $a =3D new FixPoint(34.56, 2);
> $b =3D new FixPoint(34.55, 2);
>=20
> $c =3D $a-$b;
> echo ($c);
>=20
> $c+=3D 3;
> echo $c;
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Fixe Point Decimal Data type
am 17.12.2009 16:58:02 von LinuxManMikeC
On Thu, Dec 17, 2009 at 7:49 AM, Floyd Resler wrote:
> I may be over-simplifying or not completely understanding fixed point decimals, but would number_format work?
>
> Take care,
> Floyd
>
Over-simplifying just a touch...
http://en.wikipedia.org/wiki/Floating_point_error#Accuracy_p roblems
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Fixe Point Decimal Data type
am 17.12.2009 17:10:22 von LinuxManMikeC
On Thu, Dec 17, 2009 at 7:21 AM, Raymond Irving 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.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php