Warning: number_format() expects parameter 1 to be double, string given

Warning: number_format() expects parameter 1 to be double, string given

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

Stefan Bogdanescu

Founder & Senior Architect

Warning: number_format() expects parameter 1 to be double, string given

PHP warnings like number_format() expects parameter 1 to be double, string given are among the most persistent annoyances in WordPress development. They don't just clutter your error log; they indicate a logic gap where data types diverge from what standard library functions expect. The specific scenario you encountered involves a custom slider theme file attempting to format currency without validating the input type first.

In the code snippet provided, the logic flows as follows: it checks if the post type is gtcd, retrieves the title, and then attempts to output the price if the field exists. However, there is a critical flaw in how $fields['price'] is handled before being passed to number_format(). The function number_format() is strict about its first argument; it expects a numeric type (float or int). If that variable is stored as a string—even one that looks like a number—PHP will throw this specific warning.

The root cause in your specific case lies in the initialization of $str. Looking closely at the provided code:

if $str = floatval($str); ($post->post_type == "gtcd") {
    // ...
}

There appears to be a syntax error here. The if statement is missing the condition variable or uses an assignment where a comparison should be. Assuming the intention was to convert $str to a float, that conversion needs to happen consistently. But even if we ignore the syntax error for a moment and focus on the main culprit, the issue is passing $fields['price'] directly to number_format().

In WordPress themes, data from custom fields (ACF, Meta Boxes, etc.) often returns as strings by default, regardless of whether they represent integers or decimals. If your database stores 100.50 as a string "100.50", passing it directly to number_format() triggers the warning because the function sees a string, not a double.

The Fix: Explicit Type Casting

The most robust solution is to ensure the value is treated as a number before formatting. You can achieve this using either (float) casting or floatval().

Update your code to explicitly cast the variable:

if ($post->post_type == "gtcd") {
    the_title();
    
    if (isset($fields['price'])) {
        // Cast to float first, then format
        $price = floatval($fields['price']); 
        echo ' | <span class="price_slider">' . $symbols['currency'] . number_format($price, 2) . '</span>';
    } else {
        echo '';
    }
}

By wrapping $fields['price'] in floatval(), you force PHP to interpret the string as a numeric value. Additionally, notice the addition of the second parameter to number_format(). Without specifying decimal places (e.g., 2), PHP defaults to zero decimals for integers or rounds unpredictably based on internal precision rules. Explicitly setting the decimals ensures consistency, especially when dealing with currency which usually requires two decimal places.

Why This Happens in WordPress Themes

This pattern is common in older themes or those where custom fields are not strictly typed during registration. If you are using a plugin like Advanced Custom Fields (ACF), you might need to adjust the field type settings to "Number" instead of "Text" and ensure "Return Format" is set to "Raw" if you want to handle the math yourself, or "Float" if you want ACF to handle the casting for you.

However, relying on database storage types doesn't guarantee that the retrieved value in PHP isn't a string representation of that number. This disconnect is why defensive programming—explicitly casting variables before mathematical operations—is a best practice. It prevents subtle bugs where a user enters data via an admin UI (which often saves as strings) and then breaks the frontend display logic.

If you are looking for a more scalable solution or need to handle complex pricing structures across multiple fields, consider checking out resources on handling dynamic pricing in modern PHP frameworks. For instance, LaravelCompany offers insights into building robust e-commerce features where data typing is handled strictly from the start, avoiding these legacy WordPress pitfalls.

Preventing Future Errors

To avoid this warning recurring in other parts of your codebase, adopt a habit of validating input types before performing arithmetic or formatting operations. If you frequently deal with user-submitted data or API responses, always sanitize and cast inputs to float or int immediately upon retrieval. This small step eliminates the ambiguity between string representations of numbers and actual numeric types, keeping your error logs clean and your output reliable.

Our network