473,806 Members | 2,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

number_format() question EASY

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
9 1900
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*******@attgl obal.net
=============== ===
Feb 21 '07 #2
ED

"rynTAU" <rt****@gmail.c omwrote in message
news:11******** *************@h 3g2000cwc.googl egroups.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
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...@attg lobal.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...@attgl obal.net
=============== ===

Feb 21 '07 #4
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
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*******@attgl obal.net
=============== ===
Feb 22 '07 #6
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
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
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*******@attgl obal.net
=============== ===
Feb 22 '07 #9
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*******@attgl obal.net
=============== ===
Feb 22 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
6515
by: Russ Perry Jr | last post by:
I'm using "ID" and "Value" in the generic sense here... Let's say one page I had a <html:select> with a collection like this: <html:options collection="items" property="key" labelProperty="value"/> In this case "key" is what I mean by "ID", and "value" is what I mean by "Value" -- the "Value" is shown to the user, but the "ID" is what I plan to keep, to be saved to a database (as a reference in a table to a common table as a part of...
0
1744
by: PatchFactory Support | last post by:
Description: Professional and easy-to-use patch building environment that can help you to create instant patch packages for software and file updating. Generated patch packages are small size self-extracting executable update programs in a famous installer style with adjustable user-friendly interface and multilingual support. Enhanced with features like easy-to-use interface including a Wizard mode, powerful patch engine, integrated...
4
3626
by: NBURGAN | last post by:
We are currently searching for a reporting tool with graphics for our end users who are using Oracle's standard edition. We are not using the Oracle's AS. The tool needs to be easy to use and easy to learn. We currently use Crystal Reports. Thanks. NBurgan
2
2780
by: tencip | last post by:
Hi everyone, So, I'm running data through an array, and having a little difficulty in using the number_format function. You see, I'm trying to get a comma to appear for the thousandths place, however, whenever i use the number_format function, it also forces rounding of the decimal. Does anyone know how I can place commas in the thousandths place WITHOUT doing any decimal rounding? I have some datapoints with two decimal places,...
8
1940
by: Adam Clauss | last post by:
I have a folder containing many subfolders (and subfolders and....) all containing various .cs files. Is there any "easy" way to get them all added to the solution. Preferable would be that the folders are actually created in the Solution Explorer so that I can find things easily. Its easy to select multiple files out of a single folder, but not recursively into subfolders. Any ideas? -- Adam Clauss
1
4978
by: Mad Scientist Jr | last post by:
can someone explain how to simply populate a grid in .net ? the way i understand it, there is no more msflexgrid, and instead is this new control that has to be tied to a dataset, and it is a real pain to work with if you just want to throw some values in a grid and edit them with a textbox. i don't want to persist anything in a database, i just need a fast cheap and easy grid in memory that i can work with! i had functions in vb6 that...
409
11207
by: jacob navia | last post by:
I am trying to compile as much code in 64 bit mode as possible to test the 64 bit version of lcc-win. The problem appears now that size_t is now 64 bits. Fine. It has to be since there are objects that are more than 4GB long. The problem is, when you have in thousands of places
1
2365
by: whitey | last post by:
Hi All, How do i define the number_format function to display the currency. it is showing dollars, and i want pounds thanks
17
3580
TheServant
by: TheServant | last post by:
I have realized something which is going to be a headache in the rest of my current project. I don't know how I have not come across this before but let me show you an example: $x = number_format( 10000 ); $y = 3; echo( $x*$y ); You would expect 30,000 right? Because the first number format makes that number 10,000 it is now a string (I think) and when operated upon, it converts it to a number as if it were 10.000 (ie just 10). How can...
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10624
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5684
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.