Jump to content

Formatting floats


HuggieBear

Recommended Posts

I've been wracking my brain trying to figure this simple problem out and I can't for the life of me figure out the best way to do it.

 

I have the following array:

 

$n = array('81.0', '80.5', '70.5', '67.0', '65.5');

 

I only want to display the numbers to one decimal place if it's not a whole number, like so.

 

81

80.5

70.5

67

65.5

 

I've tried looking for a solution with sprintf(), rtrim() and number_format(), but what's the best way to do it?

 

Regards

Rich

Link to comment
Share on other sites

$n = array('81.0', '80.5', '70.5', '67.0', '65.5');

foreach($n as $v)
{
   if (strstr($v, '.'))
   {
      echo number_format($v, 1) . '<br >' . PHP_EOL;
   }
   else
   {
      echo $v . '<br >' . PHP_EOL;
   }
}

 

 

Link to comment
Share on other sites

//EDIT

Just realised what you were asking for.

$n = array('81.0', '80.5', '70.5', '70', '71.569', '67.0', '65.5', '90');
foreach($n as $v)
{
   echo (ctype_digit($v) || !substr($v, strpos($v, '.') + 1, 1) ? sprintf('$n = %0.0f', $v) : sprintf('$n = %0.1f', $v)) . '<br >' . PHP_EOL;
}

 

OUTPUT:

$n = 81
$n = 80.5
$n = 70.5
$n = 70
$n = 71.6
$n = 67
$n = 65.5
$n = 90

Link to comment
Share on other sites

I can see what you've tried to do there, but it won't quite work as the 'if' condition will always return true.  strstr() will always return the decimal point along with whatever follows it.

 

I think for want of a better solution, I'll probably go with this.

 

$n = array('81.0', '80.5', '70.5', '67.0', '65.5');

foreach($n as $v)
{
   $parts = explode(".", $v);
   echo ($parts[1]) ? $v : $parts[0];
   echo "<br />\n";
}

 

Cheers

Rich

Link to comment
Share on other sites

Found a better way:

$n = array('81.0', '80.5', '70.5', '70', '71.569', '67.0', '65.5', '90');
foreach($n as $v)
{
   echo (ctype_digit($v) ? sprintf('$n = %0.0f', $v) : sprintf('$n = %0.1f', $v)) . '<br >' . PHP_EOL;
}

 

OUTPUT:

$n = 81.0
$n = 80.5
$n = 70.5
$n = 70
$n = 71.6
$n = 67.0
$n = 65.5
$n = 90

 

That doesn't work either.  Look at the first value in the list 81.0, should display as 81, it's the same for 67.0

Link to comment
Share on other sites

I edited my post before I saw your posts, the edited version will work unless you have a number to multiple decimal places and the first decimal place is 0.

 

ie.

80.025 => 80
This is correct as it would be rounded down anyway, however
80.063 => 80
This would not work correct in the code I posted, it should be 80.1

 

If this scenario won't occur in your code my edited post will work for you.

Link to comment
Share on other sites

Found a fix for that too:

 

$n = array('81.0', '80.5', '70.5', '70.025', '70.063', '71.569', '67.0', '65.5', '90');
foreach($n as $v)
{
   echo (
         (ctype_digit($v) || !substr(sprintf('%0.1f', $v), strpos(sprintf('%0.1f', $v), '.') + 1))
            ? sprintf('$n = %0.0f', $v) 
            : sprintf('$n = %0.1f', $v)
         )
         . '<br >' . PHP_EOL;
}

 

Output:

$n = 81
$n = 80.5
$n = 70.5
$n = 70
$n = 70.1
$n = 71.6
$n = 67
$n = 65.5
$n = 90

Link to comment
Share on other sites

I think you're both over thinking it:

 

<?php
   $n = array('81.0', '80.5', '70.5', '70', '71.569', '67.0', '65.5', '90');
   foreach($n as $v)
   {
      $fv = (float)$v; // cast them as floats
      $iv = (int)$v; // also cast them as ints
      $decimal = ($iv - $fv); // obtain the decimal portion by finding the difference

      if($decimal < 0) // the difference is the fractional part, always 0 or lower
      {
         echo number_format($fv, 1) . "<br />";
      }
      else
      {
         echo $iv . "<br />";
      }
   }
?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.