Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Web Design & Development Web Design & Development
Search Search
Search for:
Tech Support Guy > > >

number_format duplicity


(!)

JiminSA's Avatar
JiminSA   (Jim) JiminSA is offline JiminSA has a Profile Picture
Computer Specs
Member with 1,551 posts.
THREAD STARTER
 
Join Date: Dec 2011
Location: Johannesburg South Africa
Experience: Intermediate
11-Jun-2012, 07:45 AM #1
number_format duplicity
I'm using number_format as opposed to sprintf to format 2 fields to be comma delimited, like so...
PHP Code:
                $vehicles["vehicles"][$vehicles["nextid"]] = array(
                    
"make" => $_REQUEST["make"],
                    
"model" => $_REQUEST["model"],
                    
"year" => (int)$_REQUEST["year"],
                    
"price" => number_format(($_REQUEST["price"]), 2), 
                    
"Kms" => number_format(($_REQUEST["Kms"]), 0), 
                    
"doors" => $_REQUEST["doors"], 
as you can see one has 2 decimal places and the other does not.

Problem:
When I edit the price field the Kms field is reformatted! (i.e. any comma is treated as a decimal point - so that a value of 155,000 becomes 155)
Conversely...
When I edit the Kms field the price field is reformatted! (i.e. any comma is treated as a decimal point - so that a value of 68,700.00 becomes 68.00)

If I edit both fields before form submission it formats both correctly

How on earth should one field's being edited affect another and how can I get around this, baring in mind that I need comma delimiters for both fields, so sprintf is out...

Last edited by JiminSA; 11-Jun-2012 at 08:59 AM.. Reason: More Info...
allnodcoms's Avatar
allnodcoms   (Danny) allnodcoms is offline allnodcoms has a Profile Picture
Computer Specs
Member with 606 posts.
 
Join Date: Jun 2007
Location: Hertfordshire - England
Experience: Advanced
11-Jun-2012, 02:23 PM #2
Where's this happening Jim? Is this array used to prep the HTML page, or to serve up the DB server side?

Danny
JiminSA's Avatar
JiminSA   (Jim) JiminSA is offline JiminSA has a Profile Picture
Computer Specs
Member with 1,551 posts.
THREAD STARTER
 
Join Date: Dec 2011
Location: Johannesburg South Africa
Experience: Intermediate
12-Jun-2012, 02:30 AM #3
It's a CMS prep page array. Do you think I might find a more appropriate answer in the CMS forum? (no disrespect). Here is the generated html...
HTML Code:
</td><td> <div class="formitem">
			<div class="formitemtitle">Price </div>
			<input class="text" style="width: 100px;" type="text" id="f7_price" name="price" value="68,700.00" />
			<div class="formitemdesc">o.n.c.o. amount.</div>
			</div>
</td><td> <div class="formitem">
			<div class="formitemtitle">Kilometres</div>
			<input class="text" style="width: 100px;" type="text" id="f8_Kms" name="Kms" value="155,000" />
			<div class="formitemdesc">Odometer reading.</div>
			</div>
</td> </tr></table>

Last edited by JiminSA; 12-Jun-2012 at 02:48 AM.. Reason: more info
allnodcoms's Avatar
allnodcoms   (Danny) allnodcoms is offline allnodcoms has a Profile Picture
Computer Specs
Member with 606 posts.
 
Join Date: Jun 2007
Location: Hertfordshire - England
Experience: Advanced
12-Jun-2012, 05:37 AM #4
OK, next question... Do you use number_format() again on the receiving end? Remember that PHP uses strings to hold values, and they are interpreted as required. number_format() should only be used to pretty up output to HTML, it converts an integer to a more readable string for printing (echoing) out.

When you get your values on the server just leave 'em be, PHP knows what you're talking about and will interpret them correctly without the dots and commas.

Danny
JiminSA's Avatar
JiminSA   (Jim) JiminSA is offline JiminSA has a Profile Picture
Computer Specs
Member with 1,551 posts.
THREAD STARTER
 
Join Date: Dec 2011
Location: Johannesburg South Africa
Experience: Intermediate
12-Jun-2012, 05:56 AM #5
Aha now me unnerstan'
But, did you know that number_format pretties-up the data fields in the ensuing data records? viz:-
Code:
s:4:"year";i:2012;s:5:"price";s:9:"68,700.00";s:3:"Kms";s:7:"155,000";
- snippet of the unserialised record...

Quote:
Do you use number_format() again on the receiving end?
Yes
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑