# [DISCUSSION] bcmath: adding bcgetopts() and bcsetopts() to get or set bcmath options

- Thread: https://externals.io/message/128076
- Original archive: https://news-web.php.net/php.internals/128076
- Started by Quake MMO on 2025-07-16T11:29:15+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #128076 — Quake MMO — 2025-07-16T11:29:15+00:00

URL: https://externals.io/message/128076#128076

Hello all,

Would adding a bcgetopts(): int and a bcsetopts(int $opts): void calls 
be useful?

At this time there's no way to influence bcmath's behavior other than 
 `bcscale()`.

An example of option that could be configurable through this mechanism 
would be automatic removal of trailing / leading zeros in the results 
returned by bcmath.

bcscale(10);
echo bcadd('1', '2');
result: 3.0000000

  If we could tell bcmath to strip the trailing zeros by calling

bcsetopts(bcgetopts() | BC_STRIP_TRAILING_ZEROS));


, the result should be just '3'.



Some options I could think of:
BC_STRIP_TRAILING_ZEROS
BC_STRIP_LEADING_ZEROS
BC_STRIP_ZERO_INTEGER (so instead of returning 0.5, it would just return .5)


The actual code to strip the trailing/leading zeros seems to already be 
in the library, just not exposed to the API (bc_rm_*() in 
libbcmath/src/rmzero.c).

I'm using PHP's bcmath extensively in a performance-sensitive 
application and sanitizing the results in PHP is a performance sink that 
could be avoided this way.

Thank you for your attention.

---

## #128077 — Ayesh Karunaratne — 2025-07-16T11:56:55+00:00

URL: https://externals.io/message/128076#128077 · In reply to: #128076

> Would adding a bcgetopts(): int and a bcsetopts(int $opts): void calls
> be useful?
>
> At this time there's no way to influence bcmath's behavior other than
> `bcscale()`.
>

How about adding these as methods/properties to the `BcMath\Number` class?

`BcMath\Number` does not use the `bcmath.scale` INI directive, and in
my opinion, it is the correct way to do it. Everything that affects
the operations are just there in the code, and there is no hidden
global state.

---

## #128083 — Quake MMO — 2025-07-16T13:40:34+00:00

URL: https://externals.io/message/128076#128083 · In reply to: #128077

> > […]
> How about adding these as methods/properties to the `BcMath\Number` class?
>
> `BcMath\Number` does not use the `bcmath.scale` INI directive, and in
> my opinion, it is the correct way to do it. Everything that affects
> the operations are just there in the code, and there is no hidden
> global state.


That would also totally work. Anything that would allow sanitizing the 
string directly in the extension as opposed to doing it in PHP would be 
a great progress.

I would vouch for the sanitization switch to be a global state setting 
such as the bcscale, as opposed to (only) be a callable method/function. 
This way, we would avoid an extra call from PHP for sanitization.
