There seems to be as many gotchas with bcmath as floats. Found what i want:
****
function roundPrecision($value, $precision=12 ){
$round = $precision - floor(log10(abs($value))) - 1;
return round($value, $round);
}
****
BTW this rounding only occurs on the final result to be presented to the user.
It stops one mile being converted to 63360.00000000405 inches (at least in
Javascript).
Now to validate the original input number for LOCALE and convert it to a
DOUBLE before the calculations take place. I have seen techniques for doing
this in both PHP and Javascript during my research.
On Tuesday 07 September 2010 17:21, Simon J Welsh wrote:
> The big warning box on the float type page (http://php.net/float) suggest
> bc (http://www.php.net/manual/en/ref.bc.php)
>
> ---
> Simon Welsh
> Sent from my iPad
>
> On 7/09/2010, at 17:12, Michael Adams <[email protected]> wrote:
> > In Javascript i solved this issue (after two years) by using:
> >
> > ****
> > function round(number) {
> > return parseFloat(number.toPrecision(12));
> > }
> > ****
> >
> > The "toPrecision(12)" rounds to 12 significant digits which is well
> > within acceptable tolerances for my practical application:
> > http://www.quickconvert.net/index.html
> > http://www.quickconvert.net/javascript/calculate.js
> >
> > It does however leave trailing zeros below the decimal point. I
> > found "parseFloat()" stripped those nicely.
> >
> > Is there a PHP equivalent way to tidy double float math? I intend to
> > offer a real form alternative to those not using Javascript on this site.
> >
> > --
> > NZ PHP Users Group: http://groups.google.com/group/nzphpug
> > To post, send email to [email protected]
> > To unsubscribe, send email to
> > [email protected]
--
NZ PHP Users Group: http://groups.google.com/group/nzphpug
To post, send email to [email protected]
To unsubscribe, send email to
[email protected]