470,864 Members | 2,010 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,864 developers. It's quick & easy.

Storing formatted numeric data

Hi there,

I am wanting to store price data as n.nn format, so if the user enters "1"
the data that gets stored is "1.00"

Is there a way to do this.

Cheers

Steven
Jul 17 '05 #1
8 2037
.oO(Steven)
I am wanting to store price data as n.nn format, so if the user enters "1"
the data that gets stored is "1.00"

Is there a way to do this.


http://www.php.net/number_format

Micha
Jul 17 '05 #2
Hi there,

Thnaks for that, I have tried using it to format the number as it is
submitted to the database, but it doesnt seem to work.

I can format a number coming out of the database easy enough, but want to
do the conversion on the way in.

Has anyone used number_format() to format the form data ie:

<input type="text" name="first_value" size="20" value="<?php echo
$first_value; ?>">

All this seems to do is format the initial value to n.nn rather than the
number entered into the form field.

cheers

Steven
"Michael Fesser" <ne*****@gmx.net> wrote in message
news:qi********************************@4ax.com...
.oO(Steven)
I am wanting to store price data as n.nn format, so if the user enters "1"the data that gets stored is "1.00"

Is there a way to do this.


http://www.php.net/number_format

Micha

Jul 17 '05 #3
You should be able to format the number prior to the database insertion
query using number_format for example:
$first_value = number_format($first_value, 2);
then insert it into the database.
Steven wrote:
Hi there,

Thnaks for that, I have tried using it to format the number as it is
submitted to the database, but it doesnt seem to work.

I can format a number coming out of the database easy enough, but want to do the conversion on the way in.

Has anyone used number_format() to format the form data ie:

<input type="text" name="first_value" size="20" value="<?php echo
$first_value; ?>">

All this seems to do is format the initial value to n.nn rather than the number entered into the form field.

cheers

Steven
"Michael Fesser" <ne*****@gmx.net> wrote in message
news:qi********************************@4ax.com...
.oO(Steven)
I am wanting to store price data as n.nn format, so if the user
enters
"1"the data that gets stored is "1.00"

Is there a way to do this.


http://www.php.net/number_format

Micha


Jul 17 '05 #4
ad**@adamcoppin.com wrote in message news:<10**********************@c13g2000cwb.googleg roups.com>...
You should be able to format the number prior to the database insertion
query using number_format for example:
$first_value = number_format($first_value, 2);
then insert it into the database.
Steven wrote:
Hi there,

Thnaks for that, I have tried using it to format the number as it is
submitted to the database, but it doesnt seem to work.

I can format a number coming out of the database easy enough, but

want to
do the conversion on the way in.

Has anyone used number_format() to format the form data ie:

<input type="text" name="first_value" size="20" value="<?php echo
$first_value; ?>">

All this seems to do is format the initial value to n.nn rather than

the
number entered into the form field.

cheers

Steven
"Michael Fesser" <ne*****@gmx.net> wrote in message
news:qi********************************@4ax.com...
.oO(Steven)

>I am wanting to store price data as n.nn format, so if the user enters
"1" >the data that gets stored is "1.00"
>
>Is there a way to do this.

http://www.php.net/number_format

Micha


if you're using mysql, have a look at column type 'decimal'. don't
know about other db's though.

micha
Jul 17 '05 #5
Steven wrote:
I am wanting to store price data as n.nn format, so if the user enters "1"
the data that gets stored is "1.00"

Is there a way to do this.


You have to define the column as text -- not a good idea!
Defining the column as text loses you the ability to make calculations
with values in that column; you can't sum a bunch of strings;
"28.00" < "4.00"; ...
As far as the database is concerned

1 == 1.0 == 1.00 == 1.000 == 1.0000 == ...

just get the number and format it on the way out with number_format()
--
USENET would be a better place if everybody read: | to mail me: simply |
http://www.catb.org/~esr/faqs/smart-questions.html | "reply" to this post, |
http://www.netmeister.org/news/learn2quote2.html | *NO* MIME, plain text |
http://www.expita.com/nomime.html | and *NO* attachments. |
Jul 17 '05 #6

"Pedro Graca" <he****@hotpop.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...
Steven wrote:
I am wanting to store price data as n.nn format, so if the user enters
"1"
the data that gets stored is "1.00"

Is there a way to do this.
You have to define the column as text -- not a good idea!
Defining the column as text loses you the ability to make calculations
with values in that column; you can't sum a bunch of strings;
"28.00" < "4.00"; ...


Absolute rubbish! Every (competent ) database engine in the world is capable
of storing numeric fields, either as whole numbers (integers) or with
decimal places. It is a simple matter of choosing the right type. For MySQL
check out http://dev.mysql.com/doc/mysql/en/Numeric_types.html

--
Tony Marston

http://www.tonymarston.net
As far as the database is concerned

1 == 1.0 == 1.00 == 1.000 == 1.0000 == ...

just get the number and format it on the way out with number_format()
--
USENET would be a better place if everybody read: | to mail me:
simply |
http://www.catb.org/~esr/faqs/smart-questions.html | "reply" to this
post, |
http://www.netmeister.org/news/learn2quote2.html | *NO* MIME, plain
text |
http://www.expita.com/nomime.html | and *NO*
attachments. |

Jul 17 '05 #7
Tony Marston wrote:

"Pedro Graca" <he****@hotpop.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...
Steven wrote:
I am wanting to store price data as n.nn format, so if the user enters
"1"
the data that gets stored is "1.00"

Is there a way to do this.


You have to define the column as text -- not a good idea!
Defining the column as text loses you the ability to make calculations
with values in that column; you can't sum a bunch of strings;
"28.00" < "4.00"; ...


Absolute rubbish! Every (competent ) database engine in the world is capable
of storing numeric fields, either as whole numbers (integers) or with
decimal places. It is a simple matter of choosing the right type. For MySQL
check out http://dev.mysql.com/doc/mysql/en/Numeric_types.html


That's what I think: having a text column with numeric values is
absolute rubbish :-)

Hope the OP does not go that way.
mysql> create table x (n int, v int, s varchar(10));
Query OK, 0 rows affected (0.00 sec)

mysql> insert x values(1, 28, "28.00"), (2, 4, "4.00");
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from x order by v;
+------+------+-------+
| n | v | s |
+------+------+-------+
| 2 | 4 | 4.00 |
| 1 | 28 | 28.00 |
+------+------+-------+
2 rows in set (0.00 sec)

mysql> select * from x order by s;
+------+------+-------+
| n | v | s |
+------+------+-------+
| 1 | 28 | 28.00 |
| 2 | 4 | 4.00 |
+------+------+-------+
2 rows in set (0.00 sec)

mysql> drop table x;
Query OK, 0 rows affected (0.00 sec)
--
USENET would be a better place if everybody read: | to mail me: simply |
http://www.catb.org/~esr/faqs/smart-questions.html | "reply" to this post, |
http://www.netmeister.org/news/learn2quote2.html | *NO* MIME, plain text |
http://www.expita.com/nomime.html | and *NO* attachments. |
Jul 17 '05 #8
On Thu, 28 Oct 2004 13:33:12 +1300, in comp.lang.php "Steven"
<we*******@deepweb.co.nz> wrote:
| Hi there,
|
| I am wanting to store price data as n.nn format, so if the user enters "1"
| the data that gets stored is "1.00"
|
| Is there a way to do this.


set the field datatype
`price` decimal(5,2) NOT NULL default '0.00',
---------------------------------------------------------------
jn****@yourpantsbigpond.net.au : Remove your pants to reply
---------------------------------------------------------------
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by The Roys | last post: by
3 posts views Thread by TP | last post: by
7 posts views Thread by Carl G | last post: by
2 posts views Thread by Steven T. Hatton | last post: by
3 posts views Thread by Trevor.Dhu | last post: by
2 posts views Thread by Derek Hart | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.