Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP|BCMath: How to get bcscale value?

Tags:

php

scale

bcmath

How do I get the scale set in BCMath's bcscale() method?

Example:

bcscale(25);

How do I get the 25? Thanks!

like image 695
Treecj Avatar asked Jul 23 '13 11:07

Treecj


2 Answers

Update: As of PHP 7.3.0 you can just call the bcscale() function with no argument:

var_dump(bcscale()); // int(25)

Prior to PHP 7.3.0 you could at first try to read the bcmath.scale INI setting, which defaults to 0:

$scale = ini_get('bcmath.scale');

Since calling bcscale() didn't change the bcmath.scale INI setting, the only way to get the current scale factor was using a workaround like that:

$scale = strlen(bcsqrt('2')) - 2;
var_dump($scale); // int(25)
like image 163
Paulo Freitas Avatar answered Oct 04 '22 22:10

Paulo Freitas


This is fixed in PHP 7.3.

Calling bcscale(10) will return the previous scale and calling bcscale() will return the current scale.

source

like image 44
Bell Avatar answered Oct 04 '22 22:10

Bell