Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
Hi,
I have a decimal field set up, which contain values like 1.20, 1.25 and 1.00.
Is there a way to get these values displayed as 1.2, 1.25 and 1?
The default supplied formatters only seem to be capable of rounding the values to a certain unit but that's certainly no solution for me.
I need to be able to get rid of the trailing (and redundant) zero's.
Comments
Comment #1
markus_petrux CreditAttribution: markus_petrux commentedOverride the theme function used by the field formatter that you have used for your content type, or you may also implement an additional field formatter that works as you wish. See CCK Formatters module for an example. Any module can provide additional formatters for any field in the system, then these formatters are available from the "Display fields" screen, field settings form in Views, field settings form in Panels.
Comment #3
Stomper CreditAttribution: Stomper commentedI am currently theming a ubercart product page using a custom node-*.tpl.php file.
I was able to print the sale price to the screen (php print $node->sell_price) but it displayed the value with excessive trailing zeros "10.0000," I would like to trim it to "10.00." I have seen the sale price displayed elsewhere (default output, non custom tpl.php) and it has been trimmed to "10.00" but when I manually try and display it, it adds additional trailing zeros.
Is this purely a PHP issue or a another underlying issue? If it is jsut a PHP issue, any code suggestions would be appreciated.
Comment #4
iantresman CreditAttribution: iantresman commentedHow to remove trailing zeroes from CCK number decimal fields
I had this same requirement, to remove the trailing zero from some width and height measurement, but to display any other digit after the decimal point. This is my solution.
field_width
, the otherfield_height
. They are both set with a Scale=1 (which sets one decimal place).<?php print $output; ?>
. This outputs the contents of my CCK field as a text string, together with (a) any prefixes and suffixes (b) Any field rewriting added with the View field.<?php print $output; ?>
to:<?php print str_replace(".0","",$output); //Remove trailing .0 from decimals ?>
Comment #5
Stomper CreditAttribution: Stomper commentedThanks for the tip. So the issue with trailing zeros is not a bug?
Comment #6
iantresman CreditAttribution: iantresman commentedI don't think that CCK number fields let you specify the number of significant figures, but instead, requires the number of decimal places, regardless of whether the decimal is zero or non-zero.
By the way, the solution I gave will not remove the trailing zero from numbers such as 1.20, only zeroes that occur immediately after the decimal point such as 1.0, so you would need to modify your PHP accordingly. If you can be assured that there a no characters (ie no suffix) after the number, then I think that the following replacement string would work:
<?php print trim(trim($output, '0'), '.'); //Remove trailing zeroes after the decimal point ?>
You could also try taking a look at the CCK Formatters module, and the Formatted Number CCK> module.
Comment #7
tomsm CreditAttribution: tomsm commentedI want to display prices. So I only want to remove the decimals when they are 00.
For example € 5,00 should be € 5, but € 5,50 should remain the same, not € 5,5.
How can I do this?
edit: I have found a solution:
Comment #8
loparr CreditAttribution: loparr commentedwhere this code should go?
Comment #9
loparr CreditAttribution: loparr commentedComment #10
tomsm CreditAttribution: tomsm commented@loparr
The code in #7 comes from the display format of a computed field I used.
See also: http://drupal.org/project/computed_field
Comment #11
loparr CreditAttribution: loparr commentedHi,
Thank you. I don't use computed fields. I need to override price formatter output in template somehow:)
Comment #12
doublejosh CreditAttribution: doublejosh commentedBit cleaner...
Comment #13
rkent_87 CreditAttribution: rkent_87 commentedDelete this comment, I'm on D7, not D6
I'm not sure where the code in #12 is supposed to go? Should it all be in a custom module called custom_field_mods? I have tried that and while the 'Decimal No Zeros' option appears the field is not rendered.
Comment #14
osopolarIn #12 should
rtrim()
be used instead oftrim()
, otherwise0.25
would be formatted as25
.Comment #15
501cGeek CreditAttribution: 501cGeek commented@shakkei, @rkent_87 did you find a solution for this? On D7 it appears the Custom Formatters module can help with this, but I don't know what code to enter to make this happen... my desired outcome is the same as OP, display decimal values 1.20, 1.25 and 1.00 as 1.2, 1.25 and 1.
Comment #16
osopolar@501cGeek: The code mentioned above goes into a custom Module. This issue is about cck Module in Drupal 6. For Drupal 7 you may find the answer #2 in the issue Views: how to remove ending zeros from decimal field helpful.
Comment #17
501cGeek CreditAttribution: 501cGeek commented@osopolar thanks for your reply. I found this code snippet written for Custom Formatters which seems to solve my problem. (edit - changed to rtrim to remove trailing zeros only)
I also seen it recommended to cast the string to a float which apparently causes PHP to automatically trim trailing zeros. Have not tried but here are two PHP code examples I found for anyone interested:
or
either should output
37.422005
Comment #18
sambathbask CreditAttribution: sambathbask commentedHi,
I'm using Drupal 8...and I have this issue of the last digit after the decimal is a zero or zeros, here I don't need to show the zero as you mentioned above
...Can anyone give some suggestions to clear this issue and this might be hugely helpful...
Comment #19
sambathbask CreditAttribution: sambathbask as a volunteer commentedComment #20
wombatbuddy CreditAttribution: wombatbuddy commented@sambathbask, for Drupal 8
'Remove Trailing Zeros' module provides formatter that remove trailing zeros in decimal and float fields.