Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
450652c8
Commit
450652c8
authored
Jan 24, 2008
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for trunc().
parent
27b36693
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
0 deletions
+13
-0
Lib/decimal.py
Lib/decimal.py
+2
-0
Lib/test/test_decimal.py
Lib/test/test_decimal.py
+11
-0
No files found.
Lib/decimal.py
View file @
450652c8
...
...
@@ -1433,6 +1433,8 @@ class Decimal(object):
else
:
return
s
*
int
(
self
.
_int
[:
self
.
_exp
]
or
'0'
)
__trunc__
=
__int__
def
__long__
(
self
):
"""Converts to a long.
...
...
Lib/test/test_decimal.py
View file @
450652c8
...
...
@@ -1151,6 +1151,7 @@ class DecimalUsabilityTest(unittest.TestCase):
checkSameDec
(
"__floordiv__"
,
True
)
checkSameDec
(
"__hash__"
)
checkSameDec
(
"__int__"
)
checkSameDec
(
"__trunc__"
)
checkSameDec
(
"__long__"
)
checkSameDec
(
"__mod__"
,
True
)
checkSameDec
(
"__mul__"
,
True
)
...
...
@@ -1216,6 +1217,16 @@ class DecimalPythonAPItests(unittest.TestCase):
r
=
d
.
to_integral
(
ROUND_DOWN
)
self
.
assertEqual
(
Decimal
(
int
(
d
)),
r
)
def
test_trunc
(
self
):
for
x
in
range
(
-
250
,
250
):
s
=
'%0.2f'
%
(
x
/
100.0
)
# should work the same as for floats
self
.
assertEqual
(
int
(
Decimal
(
s
)),
int
(
float
(
s
)))
# should work the same as to_integral in the ROUND_DOWN mode
d
=
Decimal
(
s
)
r
=
d
.
to_integral
(
ROUND_DOWN
)
self
.
assertEqual
(
Decimal
(
trunc
(
d
)),
r
)
class
ContextAPItests
(
unittest
.
TestCase
):
def
test_pickle
(
self
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment