Skip Navigation

[Resolved] How to output decimal, or thousand separators with Toolset Fields

This thread is resolved. Here is a description of the problem and solution.

Problem:
I want to output a number with decimal and thousands separator or other syntaxes.

Solution:
This is not possible with the Toolset Number Field.

In the Toolset Number Field, you can pass a decimal number using a dot (.) as decimal separator only.

In Single Line Fields you could use any separator you like, but remember that the WordPress query threats every value as the string, unless otherwise specified.

This can influence sorting, searching and querying, and when specific characters like a comma (,) or repeating dots are present, it might not return the expected results.

the best is to have decimal values separated by a dot only

33% of people find this useful.

This support ticket is created 6 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 9 replies, has 3 voices.

Last updated by BuddyQ7155 6 years, 2 months ago.

Assisted by: Beda.

Author
Posts
#614711
sorting table.PNG

I am trying to: sort table columns based on numbers and currency.

Link to a page where the issue can be seen: hidden link

I expected to see: 'Price', 'Rate (sf/yr)', 'Rent', and 'Size (sf)' columns to sort ascending and descending by number and/or currency value.

Instead, I got: There doesn't seem to be any distinguishable format to the way it sorts both ascending and descending.

Additional Information: For the Post Fields, I used Text Fields because when using a Number Field, it wasn't allowing me the ability to add commas, decimal places, or dollar signs (when applicable). I suspect that this might be part of the problem but I ensured that the text format (e.g. 1,299) was always the same when entered in the back-end. I also had to add a '$' sign to the code when displaying the Post Fields (e.g. $ [types field='lease-rate'][/types]).

#614819

Text Fields will be sorted as strings, and then number values become "senseless" when ordered by.
This needs to be sorted as a numeric value instead.

What you display outside the ShortCode is not important for the sorting, the sorting will happen only on what you stored for that field.
So if the $ sign is not IN the Field's value, no problem.

I also see on your example that only unique posts do not match the order.
For RATE this seems the post hidden link.
This post just pops up ruin the middle of the list, but all other items are ordered perfectly fine.

In the Price Field it's a longer list of posts that are out of order.
I surely see a difference in the syntax used.
While for rate you used dots, for prices you used commas. whereas thousands usually are separated by upper dots or nothing at all (which is what I suggest, at least in the raw value).

What needs to be changed is the format with which you store the values.
It's difficult for a WordPress Query to decide wether a string should be handled as number, mixed or string.

So, either you should use Number field which lets you enter decimals as dots, or, single line fields, but if possible only enter numbers, no other signs, and if needed, always the same syntax and never double.
This means, 1,000,000 should be written 1000000 and 1000,01 can be written 1000.01 or with comma, but always the same, and never twice in the sam number.

Also, since I see that some posts are misbehaving but other work, I suggest to re-save those posts with this steps:
- Edit the post
- Remove (delete) the value stored in the number field
- Save the Post
- Re-add the desired value and re-save the post

This makes sure eventual corrupted data gets "corrected".
I just had a similar issue a few days ago where exactly this was the case.

Please can you make this test, and also consider to change the format/value?
This will avoid several other sorting issues WordPress knowingly has with strings/numbers.

#615777

Hi Beda. Thank you for this response. I had trouble completely understanding what you were instructing me to do but I think you said that using a Number field would be preferred, as is what I tried to do originally.

I'm getting the same problem of a comma when using a Number field.

It doesn't allow me to add a comma to a numeric field nor does it automatically include it when displaying the result on the front-end. That is why I elected to use a Text field as a workaround.

I believe you suggested using a number field, which I definitely prefer. How do I enter a numeric value and have commas display where and when appropriate (e.g. 2,199,000.00)?

Thanks.

#615957

either you should use Number field which lets you enter decimals as dots, or, single line fields, but if possible only enter numbers, no other signs, and if needed, always the same syntax and never double.

This means, you can use:
- number field
Only ONE dot and several numbers allowed (Classic decimal numbers)
- single line field
All syntaxes are allowed by the validation, but for later usage as number, you need to threat this field as a number field.
So, either you use comma or dot, and you do not use separators for thousands.
- generally, for numeric sorting, use numeric values. This means, thousands are not separated by apostrophe and as decimal we use a dot, or a comma, if it's a text field thats possible, but it must be the same always.

Basically, the approach of displaying or entering a value of 2 point five millions and 300 USD, is to insert a numeric value:

2500300

not

2,500,300

This last syntax will break every sort option based on WordPress, most likely, as it is not a numeric value anymore.

Please try this with a simple numeric value and using only decimal separator.

You can still use a text or numeric field, that should not matter.
What matters is the content you deliver.
That must be "pure", in this case a number.

#616129

I've entered the number exactly as you've described (2500300) as I've already indicated. It doesn't show any commas on the front-end. What am I missing?

If I want to display 2,500,300.00 how do I enter it exactly into a number field? I entered it as you've described, 2500300.00 and it displays that number exactly as it was entered, void of any commas on the front-end what so ever.

#616194

I've entered the number exactly as you've described (2500300) as I've already indicated. It doesn't show any commas on the front-end. What am I missing?

If I want to display 2,500,300.00 how do I enter it exactly into a number field? I entered it as you've described, 2500300.00 and it displays that number exactly as it was entered, void of any commas on the front-end what so ever.

#616286

I am sorry, I will try to explain this more clear:

1. Number Fields in Toolset Types take one separator: decimal dot.
Nothing else.
They will not take any other separator, they will not display any other separator.
To enter one million 3 hundred thousand you enter 1300000.
For one point 5 dollar you enter 1.5

2. Text Single Line allows it all, but, technically, a sort by is run by WordPress Query, this relies on PHP and other code, which does get confused when you have mixed values.
Hence to have successful ordering, sorting or finding, please use either numeric or string values.

This means, summing up, use only decimal dots.
Anything else, right now, is not possible.
WordPress iteslf will not order correctly by numbers like 1,200,000.
It will however order numerically if the number is a number (like 1200000).

So, this may be different in future, where we plan to add other types of fields eventually or allow more complex syntax.
But for now, to order by the fields, I suggest a numeric Field (no commas, no dots...) or a single line field with only decimal separator if required.

#616372

We have gone back and forth now so many times now only to find that what I've been saying the entire time, that commas aren't displaying, is in fact not happening because it's not possible using the default setup. Becasue please correct me if I'm wrong, if I use a number field and don't need the decimal place, am I able to enter a whole number and have a comma show? It seems the answer is no.

I see other sites that use your plugins within your showcase section, show commas and if it's done using php code or something else, so be it. That is a must when displaying long numbers for obvious reasons. I sent in my support ticket with the anticipation of someone there helping me make this happen. I myself tried to do a workaround by using text field only to not have that sort properly.

Someone there certainly knows what code needs to be added to add commas to make this possible. Can you please help me locate that so I can quickly implement it?

Thank you.

#616384

For those of you inquiring about the adding commas, I found the answer myself. Christian Cox posted the following code snippet to be added to your child theme's function.php file. Thanks, Christian.

add_shortcode( 'ts_format_number', 'ts_format_number_func');
function ts_format_number_func( $atts, $content ) {
$int = (int) do_shortcode($content);
return number_format($int, 0);
}

Then you can use it like this to format the number field value:

1
[ts_format_number][types field="price"][/types][/ts_format_number]

#952455

David, thanks for the post. I am in need of this too. I don't know why Toolset does not make this easy??? And why Beda was giving you a hard time about it. I understood your issue from your first message.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.