Go Back   HostGator Peer Support Forums > General Discussion > Off-Topic

Notices

Reply
 
Thread Tools
  #1  
Old 11-04-2008, 10:11 AM
JeremyR JeremyR is offline
Hatchling Croc
 
Join Date: Jun 2006
Posts: 40
Default Quick PHP question

I've got some simple math code that rounds numbers to the second decimal place. ( $x + $y) = $z round($z, 2)

when it rounds to a number with a zero in the second decimal place it doesn't pad it. How do i fix that? I want the zero to stay on the end.

IE: 5.204 would round to 5.20 but the code drops the last 0 and displays 5.2

Thanks for you help!
Reply With Quote
  #2  
Old 11-04-2008, 10:23 AM
imabluesman2's Avatar
imabluesman2 imabluesman2 is offline
Hatchling Croc
 
Join Date: Jul 2008
Location: Curacao NA
Posts: 44
Default Re: Quick PHP question

Padding is for strings.

But I googled it for you:

http://www.htmlite.com/php011.php

all about formatting numbers.
Reply With Quote
  #3  
Old 11-04-2008, 07:06 PM
GatorZach
HostGator Guest
 
Posts: n/a
Default Re: Quick PHP question

Did you try casting the variable as a float?
Reply With Quote
  #4  
Old 11-05-2008, 05:39 PM
gwyneth's Avatar
gwyneth gwyneth is offline
Supreme Croc
 
Join Date: Sep 2006
Location: up north
Posts: 6,843
Default Re: Quick PHP question

Quote:
Originally Posted by imabluesman2 View Post
Padding is for strings.

But I googled it for you:

http://www.htmlite.com/php011.php

all about formatting numbers.
That page's advice ends up having the same problem in one of its last examples because it uses padding.

Quote:
Originally Posted by JeremyR View Post
I've got some simple math code that rounds numbers to the second decimal place. ( $x + $y) = $z round($z, 2)

when it rounds to a number with a zero in the second decimal place it doesn't pad it. How do i fix that? I want the zero to stay on the end.

IE: 5.204 would round to 5.20 but the code drops the last 0 and displays 5.2

Thanks for you help!
Quote:
Originally Posted by GatorZach View Post
Did you try casting the variable as a float?
As Zach suggests, if you want to treat the variable as a number, you need to do this. Info at PHP: Type Juggling - Manual.

But if yours is the most common reason for showing the zero with two decimal places--to display U.S. money quantities--you could instead use PHP: money_format - Manual which in my limited experience is the best way to deal with money stuff. The variable stays a string, displays correctly, and is sort of the PHP equivalent of the spreadsheet and database vartype "money". Remember, if you do this, you MUST specify a valid locale. The linked manual page tells all.
Reply With Quote
  #5  
Old 11-05-2008, 08:27 PM
fanfavorite fanfavorite is offline
Junior Croc
 
Join Date: Jun 2006
Location: Toronto, Ontario, Canada
Posts: 194
Default Re: Quick PHP question

If you are working with money as the rounded number use money_format or if you are working with regular numbers use number_format.
Reply With Quote
  #6  
Old 11-06-2008, 01:33 PM
imabluesman2's Avatar
imabluesman2 imabluesman2 is offline
Hatchling Croc
 
Join Date: Jul 2008
Location: Curacao NA
Posts: 44
Default Re: Quick PHP question

Quote:
Originally Posted by gwyneth View Post
That page's advice ends up having the same problem in one of its last examples because it uses padding.
The example of printf () does exactly what JeremyR wants, I trust he's smart enough not to use the round (or abs) functions since those gave him the problem in the first place:

Code:
 <?php
$first_number = 10;
$second_number = 3;
$total_sum = $first_number + $second_number;
printf ("%01.2f", $total_sum);
?>
Outcome :

13.00
Reply With Quote
Reply

Bookmarks

Thread Tools

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
quick question nfrederick Pre-Sales Questions 8 03-07-2008 06:34 PM
Hey quick question Private_Ale Pre-Sales Questions 2 12-07-2007 09:24 PM
Quick DNS question iamaelephant Pre-Sales Questions 7 06-01-2007 06:49 PM
Quick question hxc Shared Hosting Support 2 02-28-2007 09:41 AM
Quick question Grumpy Shared Hosting Support 3 02-05-2005 12:02 PM

All times are GMT -5. The time now is 02:42 AM.