Bluehost.com Web Hosting $6.95

How to convert a column of integers to float(4.2)??

This is a discussion on How to convert a column of integers to float(4.2)?? within the MySQL Database forums, part of the Database Forums category; This is a newbie, and I suspect, a question with a very easy number. I'm surprised googling wasn't ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-11-2007
Pete
 
Posts: n/a
Default How to convert a column of integers to float(4.2)??

This is a newbie, and I suspect, a question with a very easy number.
I'm surprised googling wasn't much help.

How do I convert a whole column of of integers to numbers with 4
digits before and 2 digits after a decimal point?

For example, 2091 becomes 20.91 and 124 becomes 1.24 in the table?

Thanks!
Reply With Quote
  #2 (permalink)  
Old 03-11-2007
Paul Lautman
 
Posts: n/a
Default Re: How to convert a column of integers to float(4.2)??

Pete wrote:
> This is a newbie, and I suspect, a question with a very easy number.
> I'm surprised googling wasn't much help.
>
> How do I convert a whole column of of integers to numbers with 4
> digits before and 2 digits after a decimal point?
>
> For example, 2091 becomes 20.91 and 124 becomes 1.24 in the table?
>
> Thanks!


The answer depends on how the column is defined. Is it an integer column?


Reply With Quote
  #3 (permalink)  
Old 03-12-2007
Pete
 
Posts: n/a
Default Re: How to convert a column of integers to float(4.2)??


>The answer depends on how the column is defined. Is it an integer column?


It's an integer column. I'd like to convert the entire column from
integer to float, and in the process also convert the integer values
therein to float(4.2) format.

Reply With Quote
  #4 (permalink)  
Old 03-12-2007
Jerry Stuckle
 
Posts: n/a
Default Re: How to convert a column of integers to float(4.2)??

Pete wrote:
>> The answer depends on how the column is defined. Is it an integer column?

>
> It's an integer column. I'd like to convert the entire column from
> integer to float, and in the process also convert the integer values
> therein to float(4.2) format.
>


I'm not sure you really want to do this.

If it is a monetary value, chances are it's better this way. The reason
is simple - an integer is an exact value. However, a floating point
number is almost always an approximation, and subject to rounding errors.

In most cases when I'm writing a shopping cart, I keep the prices as
integers and only convert to floating point when I'm ready to display it.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
  #5 (permalink)  
Old 03-12-2007
Pete
 
Posts: n/a
Default Re: How to convert a column of integers to float(4.2)??

>
>If it is a monetary value, chances are it's better this way. The reason
>is simple - an integer is an exact value. However, a floating point
>number is almost always an approximation, and subject to rounding errors.
>
>In most cases when I'm writing a shopping cart, I keep the prices as
>integers and only convert to floating point when I'm ready to display it.


I've been googling for the past couple of hours and other people seem
to say the same thing. Is that how it's done in other database
platforms too? Is that how you and other always do it? When does one
use float then? Thank you.

Reply With Quote
  #6 (permalink)  
Old 03-12-2007
Captain Paralytic
 
Posts: n/a
Default Re: How to convert a column of integers to float(4.2)??

On 12 Mar, 05:33, Pete <joh...@joand.com> wrote:
> >If it is a monetary value, chances are it's better this way. The reason
> >is simple - an integer is an exact value. However, a floating point
> >number is almost always an approximation, and subject to rounding errors.

>
> >In most cases when I'm writing a shopping cart, I keep the prices as
> >integers and only convert to floating point when I'm ready to display it.

>
> I've been googling for the past couple of hours and other people seem
> to say the same thing. Is that how it's done in other database
> platforms too? Is that how you and other always do it? When does one
> use float then? Thank you.


The other alternative is tio use the DECIMAL data type.

Reply With Quote
  #7 (permalink)  
Old 03-12-2007
Jerry Stuckle
 
Posts: n/a
Default Re: How to convert a column of integers to float(4.2)??

Pete wrote:
>> If it is a monetary value, chances are it's better this way. The reason
>> is simple - an integer is an exact value. However, a floating point
>> number is almost always an approximation, and subject to rounding errors.
>>
>> In most cases when I'm writing a shopping cart, I keep the prices as
>> integers and only convert to floating point when I'm ready to display it.

>
> I've been googling for the past couple of hours and other people seem
> to say the same thing. Is that how it's done in other database
> platforms too? Is that how you and other always do it? When does one
> use float then? Thank you.
>


It's not just database platforms. It's almost any floating point number
on a PC (mainframes have a PACKED DECIMAL type which takes care of this).

Floating point numbers on a PC are stored as powers of 2, not as
decimals. That means 1/10 cannot be represented exactly - it is a
repeating decimal, much like 1/3 is in decimal notation.

The only fractional values which can be represented exactly are 1/2,
1/4, 3/8, etc.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
  #8 (permalink)  
Old 03-12-2007
Jerry Stuckle
 
Posts: n/a
Default Re: How to convert a column of integers to float(4.2)??

Captain Paralytic wrote:
> On 12 Mar, 05:33, Pete <joh...@joand.com> wrote:
>>> If it is a monetary value, chances are it's better this way. The reason
>>> is simple - an integer is an exact value. However, a floating point
>>> number is almost always an approximation, and subject to rounding errors.
>>> In most cases when I'm writing a shopping cart, I keep the prices as
>>> integers and only convert to floating point when I'm ready to display it.

>> I've been googling for the past couple of hours and other people seem
>> to say the same thing. Is that how it's done in other database
>> platforms too? Is that how you and other always do it? When does one
>> use float then? Thank you.

>
> The other alternative is tio use the DECIMAL data type.
>


This works only if the underlying language also supports a DECIMAL type.
Many, i.e. C/C++, would convert this to float anyway.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
  #9 (permalink)  
Old 03-12-2007
Jerry Stuckle
 
Posts: n/a
Default Re: How to convert a column of integers to float(4.2)??

Peter H. Coffin wrote:
> On Mon, 12 Mar 2007 09:30:53 -0500, Jerry Stuckle wrote:
>> The only fractional values which can be represented exactly are 1/2,
>> 1/4, 3/8, etc.

>
> As an amusing aside, I had a relative that worked maintaining a
> financial analysis package for stock portfolios on a mainframe back in
> the day. As soon as the NYSE announced that they were going to start
> trading in dollars and cents instead of dollars and fractional dollars,
> he announced his retirement plans. The transaction prices were all
> stored as 16-bit values, with the least three bits as the fraction part
> of the dollar. I think his total contribution to the conversion effort
> was to say "Scale everything by 800."
>


I can believe that, Peter.

People wonder why the NYSE didn't go to dollars and cents much earlier.
Now those who read this group do :-).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
  #10 (permalink)  
Old 03-13-2007
Pete
 
Posts: n/a
Default Re: How to convert a column of integers to float(4.2)??

On Mon, 12 Mar 2007 13:02:58 -0500, Jerry Stuckle
<jstucklex@attglobal.net> wrote:

>Peter H. Coffin wrote:
>> On Mon, 12 Mar 2007 09:30:53 -0500, Jerry Stuckle wrote:
>>> The only fractional values which can be represented exactly are 1/2,
>>> 1/4, 3/8, etc.


Ok, so integers it is.

Now how do I convert a column of float values to integers? Thanks.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 04:39 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0