Skip Navigation

[Resolved] Using comma as decimal separator in number field

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 10 months, 1 week ago.

Assisted by: Waqar.

Author
Posts
#2683578

Hi, we're using number fields in our custom post types. We can enter numbers with a dot as decimal separator, like this: 1.43 In English-speaking countries, the decimal point is usually a small dot (.) In many other countries (e.g. here in Germany), the radix point is a comma (,). How can we achieve that we can enter numbers with a comma instead of a dot? There should be an option for number fields to specify the decimal separator. Maybe there is a filter/hook available to change that? Best regards, Christian

#2683724

Hi,

Thank you for contacting us and I'd be happy to assist.

It is recommended to store the unformatted numerical values (for example 1234567.89) as custom field values so that numerical comparisons and arithmetic operations can be performed on them (if and when needed).

You can store the numerical values without the decimal separators in a "number" type custom field and then use a custom shortcode to display the numerical value with decimal separators, on the front end.
( there is no built-in option or filter available for this conversion )

Here is an example of the new shortcode that you can register:
( The code snippet below can be included through either Toolset's custom code feature https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ or through the active theme's "functions.php" file )


// Custom shortcode to display formatted number
function wp_format_number( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'number' => '',
        ),
        $atts
    );
 
    return number_format($atts['number'],2,'.',',');
 
}
add_shortcode( 'wp_format_number', 'wp_format_number' );

And after that, you'll be able to show the formatted value using this new shortcode, through a "Fields and Text" block, like this:


[wp_format_number number='[types field='post-number'][/types]']

Note: In this example, the Toolset's field shortcode ( [types field='post-number'][/types] ) is used in the new custom shortcode to convert the numerical value from the custom field with slug "post-number" and show the formatted value ( e.g. 1,234,567.89 ).

I hope this helps and please let me know if you need further assistance.

regards,
Waqar

#2683736
numerical-values.png

Hi Waqar, thank you for your quick reply. We're already using a similar approach. We are storing the unformatted numerical values in "number" type custom fields and output them through custom shortcodes in the way we need them (so a value like 1.5 becomes 1,5 with a comma instead of a dot). But: We're using a cred form to create new posts via frontend. In this cred form right now users have to enter numerical values with a dot (e.g. 1.5). But they should be able to enter numerical values with a comma as separator (e.g. 1,5) which is then regulary stored as 1.5 in the database. Is there a way to achieve this?

#2684071

Thanks for writing back.

There are several third-party script libraries available for formatting the number input field, with thousand separators, for example:
hidden link

#2684079

Hi Waqar, thank you for your assistance. Using javascript to resolve this issue doesn't make me completely happy. But it seems to be the only solution right now. In the future it should be possible to chose the separator for the number type custom field, because in the majority of countries worldwide the comma is used as decimal separator instead of a dot. That's why there should be an option for the number type custom field inside toolset rather than manipulating numbers via javascript.