def __init__(self, min_value=None, max_value=None, force_string=False,
precision=2, rounding=decimal.ROUND_HALF_UP, **kwargs):
"""
:param min_value: Validation rule for the minimum acceptable value.
:param max_value: Validation rule for the maximum acceptable value.
:param force_string: Store as a string.
:param precision: Number of decimal places to store.
:param rounding: The rounding rule from the python decimal library:
- decimal.ROUND_CEILING (towards Infinity)
- decimal.ROUND_DOWN (towards zero)
- decimal.ROUND_FLOOR (towards -Infinity)
- decimal.ROUND_HALF_DOWN (to nearest with ties going towards zero)
- decimal.ROUND_HALF_EVEN (to nearest with ties going to nearest even integer)
- decimal.ROUND_HALF_UP (to nearest with ties going away from zero)
- decimal.ROUND_UP (away from zero)
- decimal.ROUND_05UP (away from zero if last digit after rounding towards zero would have been 0 or 5; otherwise towards zero)
Defaults to: ``decimal.ROUND_HALF_UP``
"""
self.min_value = min_value
self.max_value = max_value
self.force_string = force_string
self.precision = precision
self.rounding = rounding
super(DecimalField, self).__init__(**kwargs)
python类ROUND_CEILING的实例源码
def test_pass_modify_rounding(self):
"""
By default, the filter will round up any value that is halfway
to the nearest `to_nearest` value, but this behavior can be
customized.
"""
self.assertFilterPasses(
self._filter('0.00000000001', rounding=ROUND_CEILING),
Decimal('1'),
)
def __init__(self, value='0', rounding=ROUND_CEILING):
# ????????? ??????????????
self._formatter = get_formatter()
# ?????????????? ?????
context = getcontext().copy()
context.prec = 18
context.rounding = rounding
value = Decimal(value)
self._value = value.quantize(Decimal('.1') ** self._formatter['decimal_places'], context=context)
# ????? ? ??????? ?????
self._int, self._frac = str(self._value).rsplit('.', 1)
def calculate_thw_index(temperature, relative_humidity, wind_speed):
"""
Uses the air temperature, relative humidity, and wind speed (THW = temperature-humidity-wind) to calculate a
potentially more accurate "felt-air temperature." This is not as accurate, however, as the THSW index, which
can only be calculated when solar radiation information is available. It uses `calculate_heat_index` and then
applies additional calculations to it using the wind speed. As such, it returns `None` for input temperatures below
70 degrees Fahrenheit. The additional calculations come from web forums rumored to contain the proprietary
Davis Instruments THW index formulas.
hi is the heat index as calculated by `calculate_heat_index`
WS is the wind speed in miles per hour
:param temperature: The temperature in degrees Fahrenheit
:type temperature: int | long | decimal.Decimal
:param relative_humidity: The relative humidity as a percentage (88.2 instead of 0.882)
:type relative_humidity: int | long | decimal.Decimal
:param wind_speed: The wind speed in miles per hour
:type wind_speed: int | long | decimal.Decimal
:return: The THW index temperature in degrees Fahrenheit to one decimal place, or `None` if the temperature is
less than 70F
:rtype: decimal.Decimal
"""
hi = calculate_heat_index(temperature, relative_humidity)
WS = _as_decimal(wind_speed)
if not hi:
return None
return hi - (THW_INDEX_CONSTANT * WS).quantize(ONE_TENTH, rounding=decimal.ROUND_CEILING)
# noinspection PyPep8Naming
def round_up(value):
"""
Round up is defined as the smallest number, specified to one decimal place, that is equal to
or higher than its input. For example, Round up (4.02) is 4.1; and Round up (4.00) is 4.0.
"""
return value.quantize(D('0.1'), rounding=ROUND_CEILING)
def calculate_heat_index(temperature, relative_humidity):
"""
Uses the temperature and relative humidity to calculate the heat index, the purpose of which is to represent a
"felt-air temperature" close to what a human actually feels given the temperature and humidity. This index does
not take into account the wind speed or solar radiation, and so is not the most accurate measure of a true
"feels-like" temperature. For that, see `calculate_thw_index` and `calculate_thsw_index`. The algorithm used and
its constants are sourced from http://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml. In this algorithm:
T is the temperature in degrees Fahrenheit
RH is the relative humidity percentage
This function is tested against the NOAA/NWS heat index chart found at
http://www.nws.noaa.gov/os/heat/heat_index.shtml. It returns `None` if the input temperature is less than 70F.
Experts disagree as to whether the heat index is applicable between 70F and 80F, but this function returns a heat
index calculation for those values.
:param temperature: The temperature in degrees Fahrenheit
:type temperature: int | long | decimal.Decimal
:param relative_humidity: The relative humidity as a percentage (88.2 instead of 0.882)
:type relative_humidity: int | long | decimal.Decimal
:return: The heat index temperature in degrees Fahrenheit to one decimal place, or `None` if the temperature is
less than 70F
:rtype: decimal.Decimal
"""
if temperature < HEAT_INDEX_THRESHOLD:
return None
T = temperature
RH = _as_decimal(relative_humidity)
heat_index = HI_0_5 * (T + HI_61_0 + ((T - HI_68_0) * HI_1_2) + (RH * HI_0_094))
heat_index = (heat_index + T) / TWO # This is the average
if heat_index < HI_SECOND_FORMULA_THRESHOLD:
return heat_index.quantize(ONE_TENTH, rounding=decimal.ROUND_CEILING)
heat_index = (
HI_C1 + (HI_C2 * T) + (HI_C3 * RH) + (HI_C4 * T * RH) + (HI_C5 * T * T) +
(HI_C6 * RH * RH) + (HI_C7 * T * T * RH) + (HI_C8 * T * RH * RH) + (HI_C9 * T * T * RH * RH)
)
if (HI_FIRST_ADJUSTMENT_THRESHOLD[0] <= T <= HI_FIRST_ADJUSTMENT_THRESHOLD[1] and
RH < HI_FIRST_ADJUSTMENT_THRESHOLD[2]):
heat_index -= (
((HI_13 - RH) / FOUR) * ((HI_17 - _abs(T - HI_95)) / HI_17).sqrt()
)
elif (HI_SECOND_ADJUSTMENT_THRESHOLD[0] <= T <= HI_SECOND_ADJUSTMENT_THRESHOLD[1] and
RH > HI_SECOND_ADJUSTMENT_THRESHOLD[2]):
heat_index += (
((RH - HI_85) / TEN) * ((HI_87 - T) / FIVE)
)
return heat_index.quantize(ONE_TENTH, rounding=decimal.ROUND_CEILING)
# noinspection PyPep8Naming