Wrox Programmer Forums Rounding Question
 | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
 PHP How-To Post your "How do I do this with PHP?" questions here.
 Welcome to the p2p.wrox.com Forums. You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersâ€™ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
October 6th, 2003, 01:56 PM
 Authorized User Join Date: Jun 2003 Location: San Jose, CA, USA. Posts: 32 Thanks: 0 Thanked 0 Times in 0 Posts
Rounding Question

I need a function that works with rounding. Actually it just has to do with decimal places rather than rounding. I know the rounding function works for turning something that is 5 decimal places into 2 with rounding, but what I want to do is turn 2 decimal places into 3. I.e. Take 0.13 and turn it into 0.130 for the sake on consistency. I tried round(0.13, 3) and got 0.13 back. Anyone have any ideas?

----------
~cmiller
__________________
----------
~cmiller
October 6th, 2003, 02:34 PM
Friend of Wrox
 Points: 2,570, Level: 21
 Activity: 0%

Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts

Well, 0.13 is equivalent to 0.130. The trailing zero is totally meaningless when it comes to identifying the value of the number. Just like 0000001 is just 1.

If you're trying to print the value of various numbers using a specific width/decimal place format, then you're talking about how that number is converted to a string, which is totally different.

You'll need to use the formatted output functions. printf() outputs your results directly, sprintf() returns the string so you can manipulate it or output it later.

http://www.php.net/printf
http://www.php.net/sprintf

Example:

\$number = .13;

printf("The number is %01.3f.", \$number); // outputs "The number is 0.130."

If you'd like to convert an array of numbers, look at vsprintf (vector sprintf)
http://www.php.net/vsprintf

Take care,

Nik
http://www.bigaction.org/

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is OffTrackbacks are Off Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Rounding JasperGIS Beginning VB 6 1 December 14th, 2005 03:09 PM Rounding kilika SQL Server 2000 4 June 1st, 2005 03:36 PM What's with the rounding?? kaizer BOOK: Beginning Java 2 2 December 22nd, 2003 11:36 PM Rounding in C# cjo ASP.NET 1.0 and 1.1 Basics 3 November 3rd, 2003 04:12 PM Rounding Droopy Classic ASP Basics 3 August 14th, 2003 08:45 PM

All times are GMT -4. The time now is 07:30 AM.