PHP: get number of decimal digits

PHP: get number of decimal digits

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP: get number of decimal digits

Determining the precision of a floating-point number in PHP requires navigating between string manipulation and mathematical logic. If you need to extract the count of decimal places from a value without relying on explode, you must understand how PHP represents numbers internally versus how they appear as strings. The most robust approach involves arithmetic operations that isolate the fractional part, rather than parsing the textual representation.

Isolating the Fractional Part

The core logic relies on separating the integer component from the decimal component. You can achieve this by using the floor function to strip away the decimals and then subtracting the result from the original number. This leaves you with a value strictly between 0 and 1 (or 0).

Consider the following implementation:

function getDecimalPlaces($number) {
    // Handle negative numbers and zero explicitly
    $absNumber = abs($number);
    
    // Get the fractional part
    $fractionalPart = $absNumber - floor($absNumber);
    
    if ($fractionalPart == 0) {
        return 0;
    }

    // Use logarithm to determine magnitude of fraction
    // log10(0.1) is -1, log10(0.01) is -2, etc.
    $log = log10($fractionalPart);
    
    // The absolute value of the log gives us the count of places after decimal
    return round(abs($log));
}

// Examples
echo getDecimalPlaces(123.456);   // Outputs: 3
echo getDecimalPlaces(123.0);     // Outputs: 0
echo getDecimalPlaces(0.007);     // Outputs: 3

This method avoids string conversion entirely, which prevents issues with trailing zeros that might be omitted during internal storage but preserved in a string literal. By using log10, you calculate the magnitude of the fractional part directly. If the fraction is 0.5 (which is $5 \times 10^{-1}$), the log base 10 yields -1. Taking the absolute value returns 1. Similarly, for 0.01, the result is -2, returning 2.

Handling Precision and Edge Cases

While this mathematical approach is elegant, it introduces a dependency on floating-point precision limits. PHP, like many languages, stores doubles with approximately 14 to 15 significant digits. If your number has more decimal places than the language can accurately represent, the calculation may yield incorrect results because the input itself is already approximated.

For instance, a value defined as 0.1 in binary floating-point is not exactly equal to one-tenth. While log10 handles this reasonably well for standard inputs, extreme precision requirements might necessitate arbitrary-precision libraries like BCMath or GMP. In those cases, you would convert the number to a string first using bcmul or similar functions to maintain exactness before attempting any arithmetic separation.

Another pitfall is the distinction between significant figures and stored decimal places. If a user inputs 123.400, PHP might treat it as 123.4. The mathematical method described above will return 1, reflecting the internal value, not necessarily the string representation provided by the user. If your goal is to count digits including trailing zeros from a specific string format, you must normalize the input first or work strictly within a string context using regular expressions, which contradicts the constraint of avoiding explode.

When to Use This Approach

This arithmetic strategy is ideal when dealing with calculated values where the exact decimal representation matters for logic, such as currency rounding or scientific calculations. It provides a predictable output based on the actual value held in memory. However, if you are processing user input from a form that explicitly requires validating a specific string format (e.g., ensuring exactly two decimals were entered), relying solely on arithmetic might hide formatting errors where the user typed .50 but it was cast to a float losing the trailing zero information.

For high-volume financial applications where exactness is non-negotiable, consider integrating with packages that offer decimal types natively or leveraging Laravel's built-in helpers if you are within an application framework. For general purpose scripting and utility functions, the logarithmic subtraction method offers a clean, dependency-free solution that respects the constraints of standard PHP floats.

Our network