# number_format() question EASY

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
This is an easy question, I'm sure but I can't seem to figure it
out.

I'm trying to use the number_format() function to remove all decimal
places from a number.

\$num = number_format(\$num, 0, '.', '');

So if I have 23.34567 I want 23, and 45.8789 will return with 45.
The problem is that the this function rounds the number off.  so that
45.8789 returns 46 instead of 45.

Is there another function?  What should I be using.

## Re: number_format() question EASY

rynTAU wrote:

\$num = int(\$num);

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: number_format() question EASY

Thanks, I knew it was something so easy that I couldn't find anything

## Re: number_format() question EASY

Jerry Stuckle wrote:

I would advise against type casting. It can produce strage results under
some circumstances.

He would be better of using \$num = floor(\$num), that's what the function
is here for.

ceil() always rounds up, floor() always rounds down, round() rounds
according to mathematical rules (down on .0 to .4, up on .5 to .9

Bye!

## Re: number_format() question EASY

Anonymous wrote:

And int() always truncates and returns an integer - which is exactly
what he wants.  See the doc.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: number_format() question EASY

Hi

Anonymous wrote:

We don't know what the OP would like to get.

floor(-2.8) => -3
(int)-2.8 => -2

HTH, Johannes

## Re: number_format() question EASY

Johannes Vogel wrote:

int(-2.8) is -2, as you indicated.  And the operator indicated he wanted
to truncate the value, so floor() would in this case provide an
incorrect value.

Now if he said he wanted the next lower integer, your solution would be
correct.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: number_format() question EASY

Jerry Stuckle wrote:

Actually, Jerry, your code would produce an E_USER_FATAL, since there
is no built-in function called int(). Any of these will work:

\$n = 25.2829;
\$n = (int) \$n;
\$n = intval(\$n);
\$n = floor(\$n);

--
Curtis, http://dyersweb.com

## Re: number_format() question EASY

Curtis wrote:

<sheepish grin>

Been doing too much C++ lately, I guess :-)

Thanks for the correction.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: number_format() question EASY

You just need to cast to an integer, ie:

\$num= 23.34567;
\$num = (int) \$num;

\$num then equals 23,
cheers,
ED