Connecting Tech Pros Worldwide Help | Site Map

Storing formatted numeric data

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 09:20 AM
Steven
Guest
 
Posts: n/a
Default 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



  #2  
Old July 17th, 2005, 09:20 AM
Michael Fesser
Guest
 
Posts: n/a
Default 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
  #3  
Old July 17th, 2005, 09:20 AM
Steven
Guest
 
Posts: n/a
Default 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]


  #4  
Old July 17th, 2005, 09:20 AM
adam@adamcoppin.com
Guest
 
Posts: n/a
Default 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]

  #5  
Old July 17th, 2005, 09:20 AM
chotiwallah
Guest
 
Posts: n/a
Default 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
  #6  
Old July 17th, 2005, 09:20 AM
Pedro Graca
Guest
 
Posts: n/a
Default 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. |
  #7  
Old July 17th, 2005, 09:20 AM
Tony Marston
Guest
 
Posts: n/a
Default 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]


  #8  
Old July 17th, 2005, 09:21 AM
Pedro Graca
Guest
 
Posts: n/a
Default 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. |
  #9  
Old July 17th, 2005, 09:21 AM
Jeff North
Guest
 
Posts: n/a
Default 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
---------------------------------------------------------------
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.