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.


markus_petrux’s picture

Status: Active » Fixed

Override 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.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Stomper’s picture

I 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.

iantresman’s picture

How 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.

  1. I have two numerical decimal CCK fields, one called field_width, the other field_height. They are both set with a Scale=1 (which sets one decimal place).
  2. In the Views UI, in Basic settings, click on the last setting for "Theme: information". A page of information is displayed that shows the two templates for each field:
    • Field Content: Width (field_width) (ID: field_width_value)
    • Field Content: Height (field_height) (ID: field_height_value)
  3. For the CCK width field, I decided that I wanted every occurrence to have its trailing zero remove, so I noted that a new template named: "views-view-field--field-width-value.tpl.php" would be required. Other template names refer to more specific occurrences of this CCK field. For example, the template "views-view-field--nodetype--field-width-value.tpl.php" would apply to occurences that appear only in the content type called "nodetype", etc.
  4. I displayed the code from the default field template, by clicking on the link for "Field Content: Width (field_width) (ID: field_width_value)", and I then saved this file into my current theme directory, giving the file the name: "views-view-field--field-width-value.tpl.php"
  5. There is only one line of active code in the new template file: <?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.
  6. To remove any trailing zeros, all I have to do is replace any occurrence of ".0" with an empty string. Note that this will not work if your numeric decimals are set to two decimal places, in which case you would have to search for two zeroes: ".00". This also assumes that none of the extra prefixes, suffixes and rewriting, will add the string ".0" anywhere else.
  7. So, in my new template file "views-view-field--field-width-value.tpl.php", I changed the line
    • <?php print $output; ?> to:
    • <?php print str_replace(".0","",$output); //Remove trailing .0 from decimals ?>
  8. After saving, I uploaded this new modified template file, "views-view-field--field-width-value.tpl.php", to my theme directory.
  9. At the bottom of the Views Theme Information, it is necessary to click the button "Rescan template files". This must be done if any changes are made to your template files, and if you upload additional template files.
  10. The same procedure is done for the CCK height field, requiring the creation of the template file: "views-view-field--field-height-value.tpl.php", and the same code change described above.
Stomper’s picture

Thanks for the tip. So the issue with trailing zeros is not a bug?

iantresman’s picture

I 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.

tomsm’s picture

I 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:

if(floor($node_field_item['value']) == $node_field_item['value'])
$display = '€ '.number_format($node_field_item['value'],0,",",".");
$display = '€ '.number_format($node_field_item['value'],2,",",".");
loparr’s picture

where this code should go?

loparr’s picture

Status: Closed (fixed) » Active
tomsm’s picture

The code in #7 comes from the display format of a computed field I used.
See also: http://drupal.org/project/computed_field

loparr’s picture

Thank you. I don't use computed fields. I need to override price formatter output in template somehow:)

doublejosh’s picture

Bit cleaner...

* Implementation of hook_field_formatter_info().

function custom_field_mods_field_formatter_info() {
  $formatters = array(
    'trimdecimal' => array(
      'label' => t('Decimal No Zeros'),
      'field types' => array('number_decimal', 'number_float'),
      'description' => t('Trim the zeros from the end of a decimal.'),
  return $formatters;

* Implementation of hook_theme().
function custom_field_mods_theme() {
  $theme = array(
    'custom_field_mods_formatter_trimdecimal' => array(
      'arguments' => array('element' => NULL),
  return $theme;

* Theming functions for our formatters
function theme_custom_field_mods_formatter_trimdecimal($element) {
  $value = $element['#item']['value'];
  $output = '';
  if (!empty($value) && is_numeric($value)) {
    $field = content_fields($element['#field_name'], $element['#type_name']);
    $output = trim(trim($value, '0'), '.');
    $prefixes = isset($field['prefix']) ? array_map('content_filter_xss', explode('|', $field['prefix'])) : array('');
    $suffixes = isset($field['suffix']) ? array_map('content_filter_xss', explode('|', $field['suffix'])) : array('');
    $prefix = (count($prefixes) > 1) ? format_plural($value, $prefixes[0], $prefixes[1]) : $prefixes[0];
    $suffix = (count($suffixes) > 1) ? format_plural($value, $suffixes[0], $suffixes[1]) : $suffixes[0];
    $output = $prefix . $output . $suffix;
  return $output;
rkent_87’s picture

Issue summary: View changes

Delete 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.

osopolar’s picture

In #12 should rtrim() be used instead of trim(), otherwise 0.25 would be formatted as 25.

501cGeek’s picture

@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.

osopolar’s picture

@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.

501cGeek’s picture

@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)

$output = '';
foreach ($variables['#items'] as $item) {
  if(strpos($item['value'],".") !== false) {
    $output .= rtrim(rtrim($item['value'], '0'), '.');
  else {
    $output .= $item['value'];
return $output;

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:

 echo floatval( "37.422005000000000000000000000000" );


$string = "37.422005000000000000000000000000";
echo (float)$string;

either should output 37.422005