mirror of
https://github.com/gristlabs/grist-core.git
synced 2024-10-27 20:44:07 +00:00
(core) Add PY2_ROUND function for easier migration
Summary: As discussed in https://github.com/dsagal/grist-help/pull/112 Test Plan: doctest in function Reviewers: paulfitz, dsagal Reviewed By: paulfitz Subscribers: dsagal Differential Revision: https://phab.getgrist.com/D3147
This commit is contained in:
parent
fc50079e03
commit
dc52c7fe0a
@ -600,12 +600,20 @@ def ROMAN(number, form_unused=None):
|
|||||||
|
|
||||||
def ROUND(value, places=0):
|
def ROUND(value, places=0):
|
||||||
"""
|
"""
|
||||||
Rounds a number to a certain number of decimal places according to standard rules.
|
Rounds a number to a certain number of decimal places,
|
||||||
|
by default to the nearest whole number if the number of places is not given.
|
||||||
|
|
||||||
>>> ROUND(2.15, 1) # Excel actually gives the more correct 2.2
|
Rounds away from zero ('up' for positive numbers)
|
||||||
2.1
|
in the case of a tie, i.e. when the last digit is 5.
|
||||||
>>> ROUND(2.149, 1)
|
|
||||||
2.1
|
>>> ROUND(1.4)
|
||||||
|
1.0
|
||||||
|
>>> ROUND(1.5)
|
||||||
|
2.0
|
||||||
|
>>> ROUND(2.5)
|
||||||
|
3.0
|
||||||
|
>>> ROUND(2.15, 1)
|
||||||
|
2.2
|
||||||
>>> ROUND(-1.475, 2)
|
>>> ROUND(-1.475, 2)
|
||||||
-1.48
|
-1.48
|
||||||
>>> ROUND(21.5, -1)
|
>>> ROUND(21.5, -1)
|
||||||
@ -616,10 +624,15 @@ def ROUND(value, places=0):
|
|||||||
0.0
|
0.0
|
||||||
>>> ROUND(-50.55,-2)
|
>>> ROUND(-50.55,-2)
|
||||||
-100.0
|
-100.0
|
||||||
|
>>> ROUND(0)
|
||||||
|
0.0
|
||||||
"""
|
"""
|
||||||
# TODO: Excel manages to round 2.15 to 2.2, but Python sees 2.149999... and rounds to 2.1
|
p = 10 ** places
|
||||||
# (see Python notes in documentation of `round()`).
|
if value >= 0:
|
||||||
return round(value, places)
|
return float(_math.floor((value * p) + 0.5)) / p
|
||||||
|
else:
|
||||||
|
return float(_math.ceil((value * p) - 0.5)) / p
|
||||||
|
|
||||||
|
|
||||||
def ROUNDDOWN(value, places=0):
|
def ROUNDDOWN(value, places=0):
|
||||||
"""
|
"""
|
||||||
|
@ -10,6 +10,7 @@ from six import unichr
|
|||||||
from six.moves import xrange
|
from six.moves import xrange
|
||||||
|
|
||||||
from usertypes import AltText # pylint: disable=import-error
|
from usertypes import AltText # pylint: disable=import-error
|
||||||
|
from .math import ROUND
|
||||||
from .unimplemented import unimplemented
|
from .unimplemented import unimplemented
|
||||||
|
|
||||||
|
|
||||||
@ -120,7 +121,7 @@ def DOLLAR(number, decimals=2):
|
|||||||
>>> DOLLAR(10, 0)
|
>>> DOLLAR(10, 0)
|
||||||
'$10'
|
'$10'
|
||||||
"""
|
"""
|
||||||
formatted = "${:,.{}f}".format(round(abs(number), decimals), max(0, decimals))
|
formatted = "${:,.{}f}".format(ROUND(abs(number), decimals), max(0, decimals))
|
||||||
return formatted if number >= 0 else "(" + formatted + ")"
|
return formatted if number >= 0 else "(" + formatted + ")"
|
||||||
|
|
||||||
|
|
||||||
@ -195,7 +196,7 @@ def FIXED(number, decimals=2, no_commas=False):
|
|||||||
'3500'
|
'3500'
|
||||||
"""
|
"""
|
||||||
comma_flag = '' if no_commas else ','
|
comma_flag = '' if no_commas else ','
|
||||||
return "{:{}.{}f}".format(round(number, decimals), comma_flag, max(0, decimals))
|
return "{:{}.{}f}".format(ROUND(number, decimals), comma_flag, max(0, decimals))
|
||||||
|
|
||||||
|
|
||||||
def LEFT(string, num_chars=1):
|
def LEFT(string, num_chars=1):
|
||||||
|
Loading…
Reference in New Issue
Block a user