[Python-Dev] trunc()
M.-A. Lemburg
mal at egenix.com
Fri Jan 25 22:03:44 CET 2008
On 2008-01-25 21:26, Steve Holden wrote:
> Antoine Pitrou wrote:
>> Raymond Hettinger <python <at> rcn.com> writes:
>>> Go ask a dozen people if they are surprised that int(3.7) returns 3.
>>> No one will be surprised (even folks who just use Excel or VB). It
>>> is foolhardy to be a purist and rage against the existing art:
>>>
>> Well, for what it's worth, here are MySQL's own two cents:
>>
>> mysql> create table t (a int);
>> Query OK, 0 rows affected (0.00 sec)
>>
>> mysql> insert t (a) values (1.4), (1.6), (-1.6), (-1.4);
>> Query OK, 4 rows affected (0.00 sec)
>> Records: 4 Duplicates: 0 Warnings: 0
>>
>> mysql> select * from t;
>> +------+
>> | a |
>> +------+
>> | 1 |
>> | 2 |
>> | -2 |
>> | -1 |
>> +------+
>> 4 rows in set (0.00 sec)
>>
> Two points. Firstly, regarding MySQL as authoritative from a standards
> point of view is bound to lead to trouble, since they have always played
> fast and loose with the standard for reasons (I suspect) of
> implementation convenience.
>
> Second, that example isn't making use of the INT() function. I was going
> to show you result of taking the INT() of a float column containing your
> test values. That was when I found out that MySQL (5.0.41, anyway)
> doesn't implement the INT() function. What was I saying about standards?
FWIW, here's what IBM has to say to this:
http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=/com.ibm.db2.udb.admin.doc/doc/r0000814.htm
"""If the argument is a numeric-expression, the result is the same number that would occur if the argument were
assigned to a large integer column or variable. If the whole part of the argument is not within the range of integers,
an error occurs. The decimal part of the argument is truncated if present."""
AFAIK, the INTEGER() function is not part of the SQL standard, at
least not of SQL92:
http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt
The way to convert a value to an integer is by casting it to
one, e.g. CAST (X AS INTEGER). The INT() function is basically
a short-cut for this.
Regards,
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Source (#1, Jan 25 2008)
>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________
:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
More information about the Python-Dev
mailing list