Remove .0 from decimal numbers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bradlc
Forum Newbie
Posts: 2
Joined: Sun Oct 31, 2010 3:06 pm

Remove .0 from decimal numbers

Post by bradlc »

How can I remove .0 from numbers?

I currently have output looking like this 64.0 x 31.0 x 35.3 cm, which is not very nice. I would like it outputted as 64 x 31 x 35.3 cm

This is the current code:

Code: Select all

$product_size = "";
$product_size .= number_format($this->data[0]->product_length,'1','.',',');
$product_size .= ' x ';
$product_size .= number_format($this->data[0]->product_width,'1','.',',');
$product_size .= ' x ';
$product_size .= number_format($this->data[0]->product_height,'1','.',',');
$product_size .= ' cm';

$this->template = str_replace('{product_size}',$product_size,$this->template);
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: Remove .0 from decimal numbers

Post by internet-solution »

Check if a value is an integer; and if yes, then round it up to integers.

Code: Select all

if($this->data[0]->product_length==(int)$this->data[0]->product_length)
$product_size .= number_format($this->data[0]->product_length);
else
$product_size .= number_format($this->data[0]->product_length,'1','.',',');
I prefer 64.0 x 31.0 x 35.3 cm, though - the decimal points are consistent in all three values.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Remove .0 from decimal numbers

Post by greyhoundcode »

Similar approach to internet-solution but using a function ...

Code: Select all

/**
 * If $number is a "whole" number then a string representing
 * the integer is returned, otherwise it is formatted to show a 
 * single decimal point.
 */
function Smart_Number_Format($number)
{
	if ( ((int) $number) == $number)
		return (string) ((int) $number);
	
	else
		return number_format($number, 1, '.', ',');
}

// Our array of numbers...
$data = array(30, 	27.642, 29.000);

// Output the string representation
echo Smart_Number_Format($data[0]) 
	. ' x ' 
	. Smart_Number_Format($data[1])
	. ' x '
	. Smart_Number_Format($data[2])
	. ' cm';
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Remove .0 from decimal numbers

Post by twinedev »

I agree with keeping the .0, more consistent, but as to the solution, here is another method:

Code: Select all

str_replace('.0','',number_format($this->data[0]->product_length,'1','.',','))
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Remove .0 from decimal numbers

Post by greyhoundcode »

Some very unscientific benchmarking with microtime() suggests there may not be much of a performance gap between either approach.

(How bored am I?)
bradlc
Forum Newbie
Posts: 2
Joined: Sun Oct 31, 2010 3:06 pm

Re: Remove .0 from decimal numbers

Post by bradlc »

I hear what you are saying about the consistent look, but I'm into reducing visual clutter.

I went with twinedev's approach - it works perfectly thanks :D
Post Reply