• janbrokes

    (@janbrokes)


    Hello could anyone help me how to add thousand separator to this function

    this show number fe: 12345

    	<?php $key_name = get_post_custom_values($key = 'cena');
    echo $key_name[0]; ?>

    ho coul i add something like this to my php ?
    number_format($number, 2, ‘,’, ‘ ‘);

    thanks
    Jan

Viewing 11 replies - 1 through 11 (of 11 total)
  • ThemeSumo

    (@themesumo)

    Something like this should work.

    $key_name = get_post_custom_values($key = 'cena');
    $formatted = number_format($key_name, 2);
    echo $formatted;

    String Number Format

    Hope this helps.

    Thread Starter janbrokes

    (@janbrokes)

    thanks for reply

    I changed this code

    	<?php $key_name = get_post_custom_values($key = 'cena');
    $formatted = number_format($key_name,0,',',' ');
    echo $formatted; ?> K?

    but its not working – this is error message
    Warning: number_format() expects parameter 1 to be float, array given in /web/htdocs3/livitcz/home/www/wp-content/themes/Divi/search-filter/7126.php on line 85
    K?

    • This reply was modified 8 years ago by janbrokes.
    • This reply was modified 8 years ago by janbrokes.
    ThemeSumo

    (@themesumo)

    Okay, try adding float as the first parameter:

    $key_name = get_post_custom_values($key = 'cena');
    $formatted = number_format((float)$key_name, 2);
    echo $formatted;
    Thread Starter janbrokes

    (@janbrokes)

    hi, many thanks for help. But after adding (float) all number changed to “1” ??

    ThemeSumo

    (@themesumo)

    When I’m testing the above code I get a correctly formatted number using “12345” as an example.

    12,345.00

    Try testing with a static figure like $key_name = '12345';.

    • This reply was modified 8 years ago by ThemeSumo.
    • This reply was modified 8 years ago by ThemeSumo.
    Thread Starter janbrokes

    (@janbrokes)

    you are right. but where can be mistake? have you any idea?

    this code shows me correct value(number without thousands separator)

    <?php $key_name = get_post_custom_values($key = 'cena');
    echo $key_name[0]; ?>

    and this shows me 1.00

    $key_name = get_post_custom_values($key = 'cena');
    $formatted = number_format((float)$key_name, 2);
    echo $formatted;

    seems like custom value is not number….but it is

    ThemeSumo

    (@themesumo)

    Hmm, I’m struggling now.

    What is the value of your custom field, is it an array? If it is, have you tried looping through it as shown in the example over here?

    Thread Starter janbrokes

    (@janbrokes)

    Hello , thanks for help. I added code you reffered to. Now code is this

                  <?php
    
      $mykey_values = get_post_custom_values( 'cena' );
      foreach ( $mykey_values as $key => $value ) {
        echo "$value K? "; 
      }
    
    ?>

    and you can see result under the picture
    https://www.livit.cz/produkty/

    could you please help me how to change it to achieve thousands separator in number?
    thanks
    Jan

    • This reply was modified 8 years ago by janbrokes.
    ThemeSumo

    (@themesumo)

    We’re back to the number_format again, maybe something like this?

    $mykey_values = get_post_custom_values( 'cena' );
    foreach ( $mykey_values as $key => $value ) {
        $formatted = number_format((float)$value, 2);
        echo $formatted . 'K?'; 
    }
    • This reply was modified 8 years ago by ThemeSumo.
    Thread Starter janbrokes

    (@janbrokes)

    super! Thank YOU very much!
    Jan

    ThemeSumo

    (@themesumo)

    Great, we got there in the end!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Thousnad separator of custom field’ is closed to new replies.