Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 6th, 2003, 01:56 PM
Authorized User
 
Join Date: Jun 2003
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
 
Old October 6th, 2003, 02:34 PM
Friend of Wrox
 
Join Date: Jun 2003
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/





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.