PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 12th, 2008, 09:19 AM
jackjconsult jackjconsult is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 5 jackjconsult User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 3 sec
Reputation Power: 0
Floatval - How to display 2 digits after decimal point

Hi all,
I'm not too expert in PHP. I've got a code in which it uses floatval. However with floatvalue it displayes only one digit after decimal if there is only one nonzero digit after it.
E.g.

£0.50 is displayed as £0.5 (incorrect 0 is missing)
£0.55 is displayed as £0.55 (correct if vboth digits are non zero after decimal)

Can anyone let me how to fix it quickly without going too deep into PHP programming?

Tx in advance.

Reply With Quote
  #2  
Old November 12th, 2008, 09:30 AM
MrFujin's Avatar
MrFujin MrFujin is online now
Lord of the Dance
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Oct 2003
Posts: 3,130 MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 23 h 48 m 21 sec
Reputation Power: 1736
take a look on number-format
Comments on this post
ManiacDan agrees!

Reply With Quote
  #3  
Old November 12th, 2008, 10:17 AM
jackjconsult jackjconsult is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 5 jackjconsult User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 3 sec
Reputation Power: 0
Tx buddy it worked

Reply With Quote
  #4  
Old November 12th, 2008, 10:38 AM
b3n's Avatar
b3n b3n is offline
Prisoner of the Sun
Dev Shed God (5000 - 5499 posts)
 
Join Date: Jul 2004
Location: The Mews At Windsor Heights
Posts: 5,309 b3n User rank is General 21st Grade (Above 100000 Reputation Level)b3n User rank is General 21st Grade (Above 100000 Reputation Level)b3n User rank is General 21st Grade (Above 100000 Reputation Level)b3n User rank is General 21st Grade (Above 100000 Reputation Level)b3n User rank is General 21st Grade (Above 100000 Reputation Level)b3n User rank is General 21st Grade (Above 100000 Reputation Level)b3n User rank is General 21st Grade (Above 100000 Reputation Level)b3n User rank is General 21st Grade (Above 100000 Reputation Level)b3n User rank is General 21st Grade (Above 100000 Reputation Level)b3n User rank is General 21st Grade (Above 100000 Reputation Level)b3n User rank is General 21st Grade (Above 100000 Reputation Level)b3n User rank is General 21st Grade (Above 100000 Reputation Level)b3n User rank is General 21st Grade (Above 100000 Reputation Level)b3n User rank is General 21st Grade (Above 100000 Reputation Level)b3n User rank is General 21st Grade (Above 100000 Reputation Level)b3n User rank is General 21st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 8 h 2 m 23 sec
Reputation Power: 2349
Facebook Orkut
Or if you have PHP5 try money_format()
__________________
.
:: My blip.fm tunes :: Web Design Feeds :: Web Dev Feeds :: CheatSheets :: PHP :: MySQL :: 13 Moon FB App.

"All matter is merely energy condensed to a slow vibration. We are all one consciousness experiencing itself - subjectively. There is no such thing as death, life is only a dream. We are the imaginations of ourselves."
- Bill Hicks


"Truth is hidden in the subtle nature of the heart of everything, although it is invisible. One cannot see it from inside and neither from the surface. One can only live and experience it."
- Heart Sutra

Reply With Quote
  #5  
Old November 13th, 2008, 06:58 PM
BikeMaster BikeMaster is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 18 BikeMaster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 43 m 8 sec
Reputation Power: 0
number_format() is great for formatting, but could screw things up if your output isn't expecting a comma (like in an XML-driven chart.

Another alternative is to use:
PHP Code:
 $result=round(100*$number)/100 

Reply With Quote
  #6  
Old November 13th, 2008, 06:58 PM
BikeMaster BikeMaster is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 18 BikeMaster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 43 m 8 sec
Reputation Power: 0
number_format() is great for formatting, but could screw things up if your output isn't expecting a comma (like in an XML-driven chart.

Another alternative is to use:
PHP Code:
 $result=round(100*$number)/100 

Reply With Quote
  #7  
Old November 14th, 2008, 12:48 AM
Catacaustic's Avatar
Catacaustic Catacaustic is offline
Code Monkey V. 0.9
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2005
Location: A Land Down Under
Posts: 1,886 Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 15 h 12 m 20 sec
Reputation Power: 1798
Quote:
Originally Posted by BikeMaster
number_format() is great for formatting, but could screw things up if your output isn't expecting a comma (like in an XML-driven chart.


That's why there's the optional parameters on number_format. To make a purely numerical value you'd use:

Code:
$val = number_format ($val, 2, ".", "");


That adds in the decimal place, but removes the other separators for thousands, etc.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Floatval - How to display 2 digits after decimal point

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap