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

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!...

1. ## 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!
Pete Guest

2. ## 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?

Paul Guest

3. ## Re: How to convert a column of integers to float(4.2)??

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.

Pete Guest

4. ## Re: How to convert a column of integers to float(4.2)??

Pete wrote:
>
> 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.
>[/ref]

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.
net
==================
Jerry Guest

5. ## Re: How to convert a column of integers to float(4.2)??

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.

Pete Guest

6. ## Re: How to convert a column of integers to float(4.2)??

On 12 Mar, 05:33, Pete <com> wrote:

>
> 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.[/ref]

The other alternative is tio use the DECIMAL data type.

Captain Guest

7. ## Re: How to convert a column of integers to float(4.2)??

Pete wrote:
>
> 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.
>[/ref]

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.
net
==================
Jerry Guest

8. ## Re: How to convert a column of integers to float(4.2)??

Captain Paralytic wrote:
>> 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.[/ref]
>
> The other alternative is tio use the DECIMAL data type.
>[/ref]

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.
net
==================
Jerry Guest

9. ## Re: How to convert a column of integers to float(4.2)??

On Mon, 12 Mar 2007 09:30:53 -0500, Jerry Stuckle wrote:

As an amusing aside, I had a relative that worked maintaining a
financial ysis package for stock portfolios on a mainframe back in
the day. As soon as the NYSE announced that they were going to start
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."

--
Don't use this code for realtime control, for weapons systems, or for
anything else that may put life or limb at hazard. It isn't man-rated,
it isn't really thing-rated, and we don't claim that it's worth a good
G*dDamn for anything at all, at all. -- Mike Andrews, on Java compilers
Peter Guest

10. ## Re: How to convert a column of integers to float(4.2)??

Peter H. Coffin wrote:
>
> As an amusing aside, I had a relative that worked maintaining a
> financial ysis 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."
>[/ref]

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.
net
==================
Jerry Guest

11. ## Re: How to convert a column of integers to float(4.2)??

On Mon, 12 Mar 2007 13:02:58 -0500, Jerry Stuckle
<net> wrote:
[/ref][/ref]

Ok, so integers it is.

Now how do I convert a column of float values to integers? Thanks.
Pete Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•