# how do i multiply small floats with an unkown number of decimal places in php without getting zero because of scientific notation?

Okay, I found a way to solve it, so, here’s how to multiply very small floating point numbers without needing to set an explicit scale for the numbers:

``````function getDecimalPlaces(\$value) {
// first we get how many decimal places the small number has
// this code was gotten on another StackOverflow answer

\$current = \$value - floor(\$value);
for (\$decimals = 0; ceil(\$current); \$decimals++) {
\$current = (\$value * pow(10, \$decimals + 1)) - floor(\$value * pow(10, \$decimals + 1));
}
return \$decimals;
}

function multiplySmallNumbers(\$value, \$smallvalue) {

\$decimals = getDecimalPlaces(\$smallvalue); // Then we get number of decimals on it
\$smallvalue = sprintf('%.'.\$decimals.'f',\$smallvalue ); // Since bcmul uses the float values as strings, we get the number as a string with the correct number of zeroes
return (bcmul(\$value,\$smallvalue));
}
``````

CLICK HERE to find out more related problems solutions.

Scroll to Top