By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,830 Members | 1,758 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,830 IT Pros & Developers. It's quick & easy.

number_format() question EASY

P: n/a
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.

Thanks for your help.

Feb 21 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
rynTAU wrote:
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.

Thanks for your help.
$num = int($num);

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 21 '07 #2

P: n/a
ED

"rynTAU" <rt****@gmail.comwrote in message
news:11*********************@h3g2000cwc.googlegrou ps.com...
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.

Thanks for your help.
You just need to cast to an integer, ie:

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

$num then equals 23,
cheers,
ED
Feb 21 '07 #3

P: n/a
Thanks, I knew it was something so easy that I couldn't find anything
about it online. :)

On Feb 21, 10:53 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
rynTAU wrote:
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.
Thanks for your help.

$num = int($num);

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

Feb 21 '07 #4

P: n/a
Jerry Stuckle wrote:
>
rynTAU wrote:
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.

Thanks for your help.

$num = int($num);
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!
Feb 21 '07 #5

P: n/a
Anonymous wrote:
Jerry Stuckle wrote:
>rynTAU wrote:
>>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.

Thanks for your help.
$num = int($num);

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!
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.
js*******@attglobal.net
==================
Feb 22 '07 #6

P: n/a
Jerry Stuckle wrote:
rynTAU wrote:
>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.

Thanks for your help.

$num = int($num);
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
Feb 22 '07 #7

P: n/a
Hi

Anonymous wrote:
Jerry Stuckle wrote:
>$num = int($num);
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
What's about minus values?
We don't know what the OP would like to get.

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

HTH, Johannes
Feb 22 '07 #8

P: n/a
Johannes Vogel wrote:
Hi

Anonymous wrote:
>Jerry Stuckle wrote:
>>$num = int($num);
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

What's about minus values?
We don't know what the OP would like to get.

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

HTH, Johannes
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.
js*******@attglobal.net
==================
Feb 22 '07 #9

P: n/a
Curtis wrote:
Jerry Stuckle wrote:
>rynTAU wrote:
>>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.

Thanks for your help.

$num = int($num);

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
<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.
js*******@attglobal.net
==================
Feb 22 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.