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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 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? |
|
|||
|
>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. |
|
|||
|
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 ================== |
|
|||
|
>
>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. |
|
|||
|
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. |
|
|||
|
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 ================== |
|
|||
|
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 ================== |
|
|||
|
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 ================== |
|
|||
|
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. |