Support

Account

Home Forums Backend Issues (wp-admin) number_format() expects parameter 1 to be float, string given

Solving

number_format() expects parameter 1 to be float, string given

    • wpsumo

    • January 16, 2021 at 4:40 am
    <?php
    	$value = get_field('data_value');
    
    				switch ($value){
    					case null:
    						echo '-';
    						break;
    					default:
    						echo esc_attr($pre_value) . number_format($value);
    				}
    			?>

    I can’t find a solution to fix the number_format() expects parameter 1 to be float, string given. Above is a snippet of the code and using switch instead of else/if.

    I only see this in post edit where the block is used. Frontend is fine once I echo '-'; if null or empty. it’s an number field on a post type and I filter it to have thousand separator if the value is filled. But since and empty is passing a string it cause the php warning. I can not find a solution via StackOverflow, feel like I’ve tested everything.

    I assume someone played with number_format() before and faced the same issue.

  • 
    default:
      if ($value) {
        echo esc_attr($pre_value) . number_format($value);
      }
    
    • wpsumo

    • January 16, 2021 at 8:08 pm

    Thanks as always John!

    Found out my default code is working I was just loading another block which did not have an solution if empty/switch for number_fromat(); which was causing the issue.

    Below works and the same using if only, but I stick with switch easier to structure if you don’t see something I’m missing or wrong doing?

    <?php
    	$value = get_field('data_value');
    
    				switch ($value){
    					case null:
    						echo '-';
    						break;
    					default:
    						echo esc_attr($pre_value) . number_format($value);
    				}
    ?>
    <?php 
    	$value = get_field('data_value');
    
    					if ($value):
    						echo esc_attr($pre_value) . number_format($value);
    					else: echo '-';
    				endif;
    ?>
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.