Connecting Tech Pros Worldwide Forums | Help | Site Map

Storing formatted numeric data

Steven
Guest
 
Posts: n/a
#1: Jul 17 '05
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



Michael Fesser
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Storing formatted numeric data


.oO(Steven)
[color=blue]
>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.[/color]

http://www.php.net/number_format

Micha
Steven
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Storing formatted numeric data


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" <netizen@gmx.net> wrote in message
news:qig0o0tivvmjskvh6nu3bhalc4p20iu09r@4ax.com...[color=blue]
> .oO(Steven)
>[color=green]
> >I am wanting to store price data as n.nn format, so if the user enters[/color][/color]
"1"[color=blue][color=green]
> >the data that gets stored is "1.00"
> >
> >Is there a way to do this.[/color]
>
> http://www.php.net/number_format
>
> Micha[/color]


adam@adamcoppin.com
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Storing formatted numeric data


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:[color=blue]
> 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[/color]
want to[color=blue]
> 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[/color]
the[color=blue]
> number entered into the form field.
>
> cheers
>
> Steven
> "Michael Fesser" <netizen@gmx.net> wrote in message
> news:qig0o0tivvmjskvh6nu3bhalc4p20iu09r@4ax.com...[color=green]
> > .oO(Steven)
> >[color=darkred]
> > >I am wanting to store price data as n.nn format, so if the user[/color][/color][/color]
enters[color=blue]
> "1"[color=green][color=darkred]
> > >the data that gets stored is "1.00"
> > >
> > >Is there a way to do this.[/color]
> >
> > http://www.php.net/number_format
> >
> > Micha[/color][/color]

chotiwallah
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Storing formatted numeric data


adam@adamcoppin.com wrote in message news:<1098928096.189907.190480@c13g2000cwb.googleg roups.com>...[color=blue]
> 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:[color=green]
> > 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[/color]
> want to[color=green]
> > 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[/color]
> the[color=green]
> > number entered into the form field.
> >
> > cheers
> >
> > Steven
> > "Michael Fesser" <netizen@gmx.net> wrote in message
> > news:qig0o0tivvmjskvh6nu3bhalc4p20iu09r@4ax.com...[color=darkred]
> > > .oO(Steven)
> > >
> > > >I am wanting to store price data as n.nn format, so if the user[/color][/color]
> enters
> "1"[color=green][color=darkred]
> > > >the data that gets stored is "1.00"
> > > >
> > > >Is there a way to do this.
> > >
> > > http://www.php.net/number_format
> > >
> > > Micha[/color][/color][/color]

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

micha
Pedro Graca
Guest
 
Posts: n/a
#6: Jul 17 '05

re: Storing formatted numeric data


Steven wrote:[color=blue]
> 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.[/color]

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. |
Tony Marston
Guest
 
Posts: n/a
#7: Jul 17 '05

re: Storing formatted numeric data



"Pedro Graca" <hexkid@hotpop.com> wrote in message
news:slrnco1cqm.8p2.hexkid@ID-203069.user.uni-berlin.de...[color=blue]
> Steven wrote:[color=green]
>> 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.[/color]
>
> 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"; ...[/color]

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


[color=blue]
> 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. |[/color]


Pedro Graca
Guest
 
Posts: n/a
#8: Jul 17 '05

re: Storing formatted numeric data


Tony Marston wrote:[color=blue]
>
> "Pedro Graca" <hexkid@hotpop.com> wrote in message
> news:slrnco1cqm.8p2.hexkid@ID-203069.user.uni-berlin.de...[color=green]
>> Steven wrote:[color=darkred]
>>> 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.[/color]
>>
>> 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"; ...[/color]
>
> 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[/color]

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. |
Jeff North
Guest
 
Posts: n/a
#9: Jul 17 '05

re: Storing formatted numeric data


On Thu, 28 Oct 2004 13:33:12 +1300, in comp.lang.php "Steven"
<webmaster@deepweb.co.nz> wrote:
[color=blue]
>| 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.[/color]

set the field datatype
`price` decimal(5,2) NOT NULL default '0.00',
---------------------------------------------------------------
jnorth@yourpantsbigpond.net.au : Remove your pants to reply
---------------------------------------------------------------
Closed Thread