Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
PHP provides the function number_format to format numbers with decimal points and thousands separator. Actually, if there was a way, in Drupal, to customize this function at site/user levels... :-)
I believe this is not so difficult to implement. All we needs is add a couple of fields to allow admins/users customize these options (decpoint/thsndsep) to admin/settings and user/edit, maybe near the date format?
Does it worth to roll a patch for 4.7?
Comments
Comment #1
LAsan CreditAttribution: LAsan commentedNo feedback.
Version not maintained.
Comment #2
keith.smith CreditAttribution: keith.smith commentedThese settings would have to be configurable per-language (though I don't know if there is anything in core that uses them right now). This is probably a language system issue (and may be a won't fix or by design there).
Comment #3
markus_petrux CreditAttribution: markus_petrux commentedIt could be used to format view counters, etc.
If it was in core, then other modules could make use of this.
If it was part of the locale system, then:
1) the {languages} table would have to be extended with 2 fields: dec_point and thousands_sep.
2) I guess .po files for core languajes would have to include values for these new fields.
3) The language settings form would have to be extended to support these new fields as well.
4) Finally, the locale API could implement something like this:
Please, don't. If it cannot be done for D7, then let it be for the future.
Comment #4
markus_petrux CreditAttribution: markus_petrux commentedMaybe it is not a language dependent issue, since I could prefer reading text in english but read numbers with comma as decimal point, or read text in spanish and set decimal point as dot, etc. Maybe it should even be user selectable, aside from providing a site default, just like date/time formats. This is how desktop systems implement this feature. You can set the language and number format at will.
So maybe this approach could be implemented like this?
1) Change "Administer -> Site configuration -> Date and time" into "Administer -> Site configuration -> Regional settings"
2) In the "Regional settings" form and a new fieldset labeled "Numbers format" that contains fields for "decimal point" and "thousands separator" AND an option to allow users setup different options from their profiles, just like time format.
3) Extend users table with 2 fields: dec_point and thousands_sep.
4) Extend user settings form with those fields, that would show if site had enabled this option in "Regional settings".
5) Finally, common.inc could implement something like this:
I think this is better that the other approach. It gives more freedom to sites and users to set these options, regardless of language settings.
Comment #5
markus_petrux CreditAttribution: markus_petrux commentedIn the meantime I have implemented #4 as a contrib module:
http://drupal.org/project/format_number
It seems this is more country dependent than language dependent, so I opted for site default / user selected options.
http://en.wikipedia.org/wiki/Decimal_separator
Comment #6
markus_petrux CreditAttribution: markus_petrux commentedSorry for double posting.
I just wanted to mention that I've exported the whole contributions CVS just to check, and there are a lot of modules using PHP function number_format(), from CCK number field, to ecommerce, and a lot more. So maybe it would be nice to have something like this in core.
Comment #7
markus_petrux CreditAttribution: markus_petrux commentedJust bumping to add a few references I used to complete the Format Number API module, where I believe it covered all possible combinations used around the globe.
- PHP manual: number_format().
- Unicode Locale Data Markup Language (LDML).
- Unicode CLDR Project: Common Locale Data Repository (CLDR).
- CLDR By-Type Chart: number.symbol.
- Wikipedia: Decimal separator.
If it was to be included in core, one possible way would by renaming Administration / Site configuration / Date and time into "Regional settings" and there include the current settings for "Date and time" and include an additional fieldset for "Number format" settings. The same approach could be applied to the user profile page.
Then everyone would have a centralized method to format numbers, the same way there's one for dates, etc.
Worths writing a patch for this?
Comment #8
Jose Reyero CreditAttribution: Jose Reyero commentedComment #9
markus_petrux CreditAttribution: markus_petrux commentedIs it possible to consider this feature for D7?
Comment #10
plachI am afraid this will have to wait for D8 :(
Comment #11
Dave ReidAdding 'xmlsitemap' tag since I'd like to see a cleaner way of i18n numbers besides having to manually put t('0.0'), t('0.1), etc. in the module.
Comment #12
grendzy CreditAttribution: grendzy commentedI agree this is too late for 7, but would be nice for core. Check out this excerpt from xmlsitemap.de.po. Eeek!
Comment #13
grendzy CreditAttribution: grendzy commentedsorry, in-flight collision on the tag.
Comment #14
Dave ReidLet's work a great version of that Format Number API module for D7, including an interface to match what the date/time localization does in D7. Plus let's get some tests. :) That should help ease this for D8.
Comment #15
markus_petrux CreditAttribution: markus_petrux commentedPossible dup: #471058: Implement format_number() for international number formating
@Dave #14: Sounds a good plan. :)
Comment #16
Dave ReidWoo-hoo! :)
Comment #17
markus_petrux CreditAttribution: markus_petrux commentedUpdating status of the issue...
Sorry it happens quite late, but there's finally a version of Format Number API for Drupal 7. Cheers
Another issue in the Drupal project queue related to this: #1345758: META: Provide locale (regional) formats framework for automated translation of non textual data
Comment #18
wil2091 CreditAttribution: wil2091 commentedHow can I make the numeric digits to be displayed only in english format irrespective of the language selected. My website translates the numbers as well based on the language selected. Any suggestion, what am I doing wrong?
Comment #23
AnybodyThis is a very very old issue but still not solved and a big problem for several cases in multilingual websites.
Drupal 8 should clearly provide an abstraction for number_format() like it does for format_date().
It should be used instead of number_format() all modules to make it possible to flexibly override the date formatting. How can we proceed here
Comment #24
markhalliwellThis should be added to \Drupal\Component\Utility\Number instead of creating a new procedural function.
If we're talking about adding settings/config, then maybe it should be converted into a service instead (like date.formatter currently is).
Also, while i18n may need this, it really has nothing to do with the language system.
Comment #25
AnybodyI completely agree with @markcarver. How can we proceed here?
Comment #26
Yaazkal CreditAttribution: Yaazkal commented+1
Comment #27
apaderno@Anybody If you are able to provide a patch, that would be a start.
Comment #29
AnybodyYes Yaazkal, that's a good point. I think this behaves a lot like date formatting, because both is kind of core "localization" (independent from i18n because the Drupal site always has a language which doesn't have to be english).
I'll have a look at the date.formatter service implementation but could need some help because I didn't write custom services yet.
Furthermore I'm planning to write a custom module for more flexible number formatting. The current decimal formatter only supports fixed amount of decimals, which doesn't fit for many industry cases, where the decimals show the precision and if no decimal is entered then it should not be output as zero. I'll create a separate issue / module project for that, but it could also become part of the core patch later on.
If there is any core maintainer with deeper knowledge and interest to fix this, please don't hesitate to contact me.
Comment #31
AnybodyI now started a contrib project for this and would hope to see some of you help coding :)
Let's keep this issue open to integrate the module into core one day, if it succeeds. I still think this should be handled equally to date formats (central configuration + i18n)
Comment #32
henk CreditAttribution: henk at RatioWeb for Bright Solutions GmbH commented@Anybody can you share the place where you are working on it? Maybe I can help with something?
Comment #33
Anybody@henk: Thank you! See https://www.drupal.org/project/advanced_number_format - currently it's only plans / discussion / documentation, but we can start coding soon. Feel free to add comments in the issues!
Comment #37
mlncn CreditAttribution: mlncn at Agaric for Drutopia, Find It Cambridge, Cambridge, Massachusetts Family Policy Council commentedI just noticed that there doesn't seem to be a way to format the number when using formatPlural(). For example, "Your search has one result" or "Your search has 1234 results"— there's no way to format that large number as as 1,234.
I wonder if this venerable issue could help with that also?