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 26th, 2012, 01:13 AM
ohmynexus ohmynexus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 ohmynexus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 36 sec
Reputation Power: 0
Issue with prices and number types

Alrighty gents. I be in another rutt.

Users will be able to create items on my site and each item will have a price. As is, it is listed as such:

PHP Code:
echo "Price: $<input type='text' name='newDollars' maxlength='6' size='6' value='0' />.<input type='text' name='newCents' maxlength='2' size='1' value='00' /><br />"


Note the "." in between the two boxes. This represents a DOUBLE(8, 2) number type that can for example hold 123456.78.

The issue is with taking the input they give and converting it into a usable number that can be manipulated and displayed.

PHP Code:
if (is_numeric($dollars) && is_numeric($cents))
                {
                    
$dollars = (int)$dollars;
                    
$cents = (int)$cents;
                    
$price = (float)($dollars.".".$cents);
                }
                else
                {
                    
$error.= "Price must be a number.";
                } 


The code was one long line but i broke it up to make it readable. Essentially, dollars and cents are converted into an int to make sure that the user doesnt use decimals in the first or second box. These are then created into a float number where they are stored as a price.

Dollars works fine, but cents has issues.

If the user enters "1." or just "1" in cents, it is converted to "10", but if the user enters ".1" or "01" it is also converted to "10".

I think the issue is with the number type ignoring 0's, but I can't seem to fix the issue.

Is there a solution? Is there a better way to input prices on items?

Any and all help is, as always, greatly appreciated.

Reply With Quote
  #2  
Old November 26th, 2012, 03:15 AM
simplypixie simplypixie is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 104 simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 18 h 33 m 57 sec
Reputation Power: 11
Sorry, I am not male, but hopefully what I have to say will help

Try
PHP Code:
 $cents sprintf("%02d"$cents

Reply With Quote
  #3  
Old November 26th, 2012, 05:24 PM
ohmynexus ohmynexus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 ohmynexus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 36 sec
Reputation Power: 0
Sorry, when do i insert that code? in place of the (int)cents?

Reply With Quote
  #4  
Old November 27th, 2012, 01:30 AM
simplypixie simplypixie is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 104 simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 18 h 33 m 57 sec
Reputation Power: 11
Quote:
Originally Posted by ohmynexus
Sorry, when do i insert that code? in place of the (int)cents?


Yes replace (int)cents with sprintf("%02d", $cents)

Reply With Quote
  #5  
Old November 27th, 2012, 04:15 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
And it looks to me as though this should represent a DECIMAL, not a DOUBLE!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Issue with prices and number types

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