1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

number_format duplicity

Discussion in 'Web Design & Development' started by JiminSA, Jun 11, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    2,058
    I'm using number_format as opposed to sprintf to format 2 fields to be comma delimited, like so...
    PHP:
                    $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

    :confused: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...
     
  2. allnodcoms

    allnodcoms

    Joined:
    Jun 30, 2007
    Messages:
    613
    Where's this happening Jim? Is this array used to prep the HTML page, or to serve up the DB server side?

    Danny
     
  3. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    2,058
    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:
    </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>
     
  4. allnodcoms

    allnodcoms

    Joined:
    Jun 30, 2007
    Messages:
    613
    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
     
  5. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    2,058
    Aha now me unnerstan'(y)
    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...

    Yes
     
  6. Sponsor

As Seen On
As Seen On...

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.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1056629