Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
number_format duplicity
06-11-2012, 11:52 PM (This post was last modified: 06-11-2012 11:53 PM by JiminSA.)
Post: #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
Here is the generated 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>

If you don't squeak, you won't get oiled!Cool
Find all posts by this user
Quote this message in a reply
06-12-2012, 10:58 PM
Post: #2
RE: number_format duplicity
You need to remove the comma before feeding it into number_format(). It needs to be a numeric value. It is best to store a numeric value too. IMO, the admin doesn't matter how the number gets displayed - only the frontend really matters.

Author of Barebones CMS

If you found my reply to be helpful, be sure to donate!
All funding goes toward future product development.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)

© CubicleSoft