Programmatically format integer field display I am trying to programmatically format how an integer field displays. So far I have only been able to produce "1.00", and I am trying to get it like so: x.xx.x. The content type is "Project" and the field is "Drupal version".

Here is my code:

&lt;!-- Display the drupal version with decimals --&gt;
      &lt;?php if (isset($node) &amp;&amp; $node-&gt;type == 'project'): ?&gt;
          &lt;?php $drupal_version = field_view_field('node', $node, 'field_drupal_version', array('label'=&gt;'default')); ?&gt;

          &lt;?php $formatted_drupal_version = number_format((float)$drupal_version, 2); ?&gt;
          &lt;?php echo "Drupal version: " . render($formatted_drupal_version); ?&gt;&lt;/span&gt;
      &lt;?php endif; ?&gt;


output: Drupal version: 1.00

Does anyone know why this is the output, and how I can extend this function number_format to produce something in this format x.xx.x, for example "Drupal version: 7.41.1" ?

Thanks!
You can do this using the following code:

$drupal_version = (int)$drupal_version; // make sure this is an int
$parts = array();
preg_match('/^(\d{1})(\d{2})(\d+)$/', "$drupal_version", $parts);
array_shift($parts); // get rid of the first element in $parts which is the whole matched string
$formatted_drupal_version = implode($parts, '.');
echo "Drupal version: " . $formatted_drupal_version;


As examples, these integers will turn into the following strings:
7411 => 7.41.1
74110 => 7.41.10

Which means, that this code, will work parsing the provided integer from left to right, and collecting, 1 digit for the major version, 2 digits for the medium version, and all the remaining digits for the minor version.

Hope this helps!

P.S.: You question is a bit misleading, as @No Sssweat says, you cannot format an integer like that. That is why string parsing is the only way to provide you with the desired result.