Format the output of a number

Support General Format the output of a number

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #39353
    JoJo
    Participant

    Hi, I'm trying to change the output format of number custom fields (that represent prices).
    The IDs of those custom fields are
    prix_vente_bien
    loyer-mensuel_bien
    prix_net_vendeur_bien
    charges_bien
    taxe_fonciere

    I've played with the code provided there: https://docs.metabox.io/filters/rwmb-meta/ and there https://www.php.net/manual/en/function.number-format.php
    but I'm unable to make it work.
    I don't care how it shows in the custom field but I would like it to be output with this format when I pull the data in the builder (if it makes sense).

    I'm trying to have this format XXX XXX XXX,XX :

    $number = 1234.56;
    
    $nombre_format_francais = number_format($number, 2, ',', ' ');
    // 1 234,56

    Could you help me with that?
    Thanks a lot in advance!

    #39359
    PeterPeter
    Moderator

    Hello there,

    Can you please let me know the code you use to output the field value? And where did you add the code?

    I do not see any issue with the format function above. Here is an example:

    $number = rwmb_meta( 'number_9htwx774glj' );
     // French notation
    $nombre_format_francais = number_format( $number, 2, ',', ' ' );
    echo $nombre_format_francais;
    #39448
    JoJo
    Participant

    Hi Peter, thanks a lot, the code above doesn't work (I used it with the ID of my custom field). How am I suppose to put it?

    I put it like that:

    <?php 
    $number = rwmb_meta( 'my-custom-fild-id' );
    $nombre_format_francais = number_format( $number, 2, ',', ' ' );
    echo $nombre_format_francais;

    Isn't it suppose to be this way?
    Thanks a lot for your help!

    #39461
    PeterPeter
    Moderator

    Hello,

    Here is how it displays on my side https://monosnap.com/file/wsptuGxuFs6LNhEGfhHgh4s0HrJUWh

    I do not see any issue with the format code. Can you please share some screenshots of the issue on your side?

    #39465
    JoJo
    Participant

    Thanks for your help,
    here is how I inserted the code:
    https://snipboard.io/QGAcW9.jpg

    And what I get = a number displayed on top of all pages and no formatting of the field:
    https://snipboard.io/XqAiRo.jpg

    Thanks a lot for your support

    #39486
    PeterPeter
    Moderator

    Hello,

    So if you only add the code to output the field value, does it display the correct field value?

    $number = rwmb_meta( 'my-custom-field-id' );
    echo $number;

    If not, you can try to pass the post ID to the helper function to get the field value of a specific post.

    $number = rwmb_meta( 'my-custom-field-id', '', 123 );

    where 123 is the post ID. Please read more on the documentation https://docs.metabox.io/functions/rwmb-meta/

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.