Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
erp5
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Carlos Ramos Carreño
erp5
Commits
d015e4b1
Commit
d015e4b1
authored
Feb 21, 2024
by
Kazuhiko Shiozaki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
RoundingTool: add a utility round() method that supports rounding option.
parent
f41b33cd
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
1 deletion
+40
-1
bt5/erp5_trade/TestTemplateItem/portal_components/test.erp5.testRoundingTool.py
...plateItem/portal_components/test.erp5.testRoundingTool.py
+17
-0
product/ERP5/bootstrap/erp5_core/ToolComponentTemplateItem/portal_components/tool.erp5.RoundingTool.py
...tTemplateItem/portal_components/tool.erp5.RoundingTool.py
+23
-1
No files found.
bt5/erp5_trade/TestTemplateItem/portal_components/test.erp5.testRoundingTool.py
View file @
d015e4b1
...
@@ -55,6 +55,23 @@ class TestRoundingTool(ERP5TypeTestCase):
...
@@ -55,6 +55,23 @@ class TestRoundingTool(ERP5TypeTestCase):
user_folder
.
_doAddUser
(
'assignor'
,
''
,
[
'Auditor'
,
'Author'
,
'Assignor'
],
[])
user_folder
.
_doAddUser
(
'assignor'
,
''
,
[
'Auditor'
,
'Author'
,
'Assignor'
],
[])
self
.
tic
()
self
.
tic
()
def
testRoundMethod
(
self
):
"""
Test round utility method
"""
rounding_tool
=
self
.
portal
.
portal_roundings
round
=
rounding_tool
.
round
self
.
assertEqual
(
round
(
500
*
655.957
,
0
),
327979.0
)
self
.
assertEqual
(
round
(
-
0.5
),
-
1
)
self
.
assertEqual
(
round
(
0.15
,
1
),
0.2
)
self
.
assertEqual
(
round
(
-
0.15
,
1
),
-
0.2
)
self
.
assertEqual
(
round
(
1.25
,
1
),
1.3
)
self
.
assertEqual
(
round
(
500
*
655.957
,
0
,
'ROUND_HALF_EVEN'
),
327978.0
)
self
.
assertEqual
(
round
(
-
0.5
,
0
,
'ROUND_HALF_EVEN'
),
-
0.0
)
self
.
assertEqual
(
round
(
0.15
,
1
,
'ROUND_HALF_EVEN'
),
0.2
)
self
.
assertEqual
(
round
(
-
0.15
,
1
,
'ROUND_HALF_EVEN'
),
-
0.2
)
self
.
assertEqual
(
round
(
1.25
,
1
,
'ROUND_HALF_EVEN'
),
1.2
)
def
testRoundValueMethod
(
self
):
def
testRoundValueMethod
(
self
):
"""
"""
Test rounding method
Test rounding method
...
...
product/ERP5/bootstrap/erp5_core/ToolComponentTemplateItem/portal_components/tool.erp5.RoundingTool.py
View file @
d015e4b1
...
@@ -30,7 +30,7 @@ import zope.interface
...
@@ -30,7 +30,7 @@ import zope.interface
from
AccessControl
import
ClassSecurityInfo
from
AccessControl
import
ClassSecurityInfo
from
Products.ERP5Type.Tool.BaseTool
import
BaseTool
from
Products.ERP5Type.Tool.BaseTool
import
BaseTool
from
erp5.component.interface.IRoundingTool
import
IRoundingTool
from
erp5.component.interface.IRoundingTool
import
IRoundingTool
from
decimal
import
(
ROUND_DOWN
,
ROUND_UP
,
ROUND_CEILING
,
ROUND_FLOOR
,
from
decimal
import
(
Decimal
,
ROUND_DOWN
,
ROUND_UP
,
ROUND_CEILING
,
ROUND_FLOOR
,
ROUND_HALF_DOWN
,
ROUND_HALF_EVEN
,
ROUND_HALF_UP
)
ROUND_HALF_DOWN
,
ROUND_HALF_EVEN
,
ROUND_HALF_UP
)
ROUNDING_OPTION_DICT
=
{
'ROUND_DOWN'
:
ROUND_DOWN
,
ROUNDING_OPTION_DICT
=
{
'ROUND_DOWN'
:
ROUND_DOWN
,
...
@@ -41,6 +41,24 @@ ROUNDING_OPTION_DICT = {'ROUND_DOWN':ROUND_DOWN,
...
@@ -41,6 +41,24 @@ ROUNDING_OPTION_DICT = {'ROUND_DOWN':ROUND_DOWN,
'ROUND_HALF_EVEN'
:
ROUND_HALF_EVEN
,
'ROUND_HALF_EVEN'
:
ROUND_HALF_EVEN
,
'ROUND_HALF_UP'
:
ROUND_HALF_UP
}
'ROUND_HALF_UP'
:
ROUND_HALF_UP
}
def
round
(
value
,
ndigits
=
None
,
decimal_rounding_option
=
'ROUND_HALF_UP'
):
if
ndigits
is
None
:
precision
=
1
else
:
assert
isinstance
(
ndigits
,
int
),
'ndigits should be int.'
precision
=
10
**
-
ndigits
if
precision
>=
1
:
value
=
Decimal
(
str
(
value
))
value
/=
precision
value
=
value
.
quantize
(
precision
,
rounding
=
decimal_rounding_option
)
value
*=
precision
result
=
float
(
value
.
quantize
(
precision
))
else
:
result
=
float
(
Decimal
(
str
(
value
)).
quantize
(
Decimal
(
str
(
precision
)),
rounding
=
decimal_rounding_option
))
return
result
@
zope
.
interface
.
implementer
(
IRoundingTool
)
@
zope
.
interface
.
implementer
(
IRoundingTool
)
class
RoundingTool
(
BaseTool
):
class
RoundingTool
(
BaseTool
):
"""Rounding Tool"""
"""Rounding Tool"""
...
@@ -113,3 +131,7 @@ class RoundingTool(BaseTool):
...
@@ -113,3 +131,7 @@ class RoundingTool(BaseTool):
by python standard decimal module.
by python standard decimal module.
"""
"""
return
ROUNDING_OPTION_DICT
.
items
()
return
ROUNDING_OPTION_DICT
.
items
()
security
.
declarePublic
(
'round'
)
def
round
(
self
,
value
,
ndigits
=
None
,
decimal_rounding_option
=
'ROUND_HALF_UP'
):
return
round
(
value
,
ndigits
,
decimal_rounding_option
)
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