Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
Register
| 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 .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 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
Default 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
Reply With Quote
  #2 (permalink)  
Old October 6th, 2003, 02:34 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.