Showing decimal field leave extra zeros at the end. Any filter to solve that?

Support MB Custom Table Showing decimal field leave extra zeros at the end. Any filter to solve that?Resolved

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #13386
    Camilo MejiaCamilo Mejia
    Participant

    I'm using MB Custom Table plugin and Elementor to display decimal fields. I used decimal(10, 2) when I created the table. The problem is that the numbers are displayed this way:

    80.53
    50.20
    20.00

    What I need is to display them without extra zeros at the end:

    80.53
    50.2
    20

    I tried with the filters get_post_metadata, rwmb_get_value and rwmb_meta without success, they didn't dispatch with fields on a custom table. I also looked over the source code trying to find something useful without success.

    What can I do?

    #13541
    Camilo MejiaCamilo Mejia
    Participant

    Any idea?

    #13543
    Anh TranAnh Tran
    Keymaster

    Hi Camilo,

    As you set the data format for the table, can you please check the values stored in the database? If they're saved in that format, then all plugins are working properly.

    #13701
    Camilo MejiaCamilo Mejia
    Participant

    Thanks for your help, but that's not what I'm asking. What I need is to remove the extra leading zeros. With custom fields, we can use a filter to format the data as we need, but when using MB Custom Table, the filters aren't triggered, so I don't know how to format the data before showing to the user.

    #13705
    Anh TranAnh Tran
    Keymaster

    Hi Camilo,

    I got it. So if you use the helper function rwmb_meta to get the value, you can use a filter to remove the extra zeros. This is the filter:

    apply_filters( 'rwmb_meta', $meta, $key, $args, $post_id );

    So you can do something like this:

    add_filter( 'rwmb_meta', function( $meta, $key, $args, $post_id ) {
        if ( 'your_field_id' === $key ) {
            $meta = your_function_remove_extra_zeros( $meta );
        }
        return $meta;
    }, 10, 4 );
    #13721
    Camilo MejiaCamilo Mejia
    Participant

    I tried that previously but isn't working when used with Elementor dynamic fields.

    #13724
    Camilo MejiaCamilo Mejia
    Participant

    Looking at the code in meta-box/inc/functions.php I found the filter rwmb_the_value. That was the filter I was looking for. That filter is missing in the documentation.

    #13747
    Anh TranAnh Tran
    Keymaster

    Glad that you found the filter. I missed it, too. Just added the docs for it. Thanks for your feedback!

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