Python decimal.ROUND_HALF_DOWN Examples

The following are 7 code examples of decimal.ROUND_HALF_DOWN(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module decimal , or try the search function .
Example #1
Source File: pos_client.py    From hwk-mirror with GNU General Public License v3.0 6 votes vote down vote up
def create_order(ordered_items, total, payment_type):
        """create a modified order"""
        order_dct = {
            "items": ordered_items,
            "payment_type":payment_type,
            "total": total}
        
        tax_scale = int((Order.taxrate * 100) + 10000)
        subtotal = int(decimal.Decimal((total * 100) 
                / tax_scale).quantize(
                decimal.Decimal('0.01'),
                    rounding=decimal.ROUND_HALF_DOWN) * 100)

        tax = total - subtotal
        order_dct["subtotal"] = subtotal
        order_dct["tax"] = tax
        return order_dct 
Example #2
Source File: balancing.py    From python-mysql-pool with MIT License 5 votes vote down vote up
def _calc_ratio(part, whole):
    """Calculate ratio

    Returns int
    """
    return int((part / whole * 100).quantize(
        decimal.Decimal('1'), rounding=decimal.ROUND_HALF_DOWN)) 
Example #3
Source File: utilities.py    From gprMax with GNU General Public License v3.0 5 votes vote down vote up
def round_value(value, decimalplaces=0):
    """Rounding function.

    Args:
        value (float): Number to round.
        decimalplaces (int): Number of decimal places of float to represent rounded value.

    Returns:
        rounded (int/float): Rounded value.
    """

    # Rounds to nearest integer (half values are rounded downwards)
    if decimalplaces == 0:
        rounded = int(d.Decimal(value).quantize(d.Decimal('1'), rounding=d.ROUND_HALF_DOWN))

    # Rounds down to nearest float represented by number of decimal places
    else:
        precision = '1.{places}'.format(places='0' * decimalplaces)
        rounded = float(d.Decimal(value).quantize(d.Decimal(precision), rounding=d.ROUND_FLOOR))

    return rounded 
Example #4
Source File: metaclass.py    From hwk-mirror with GNU General Public License v3.0 5 votes vote down vote up
def _subtotal(self)->int:
        tax_scale = int((self.taxrate * 100) + 10000)
        result = Decimal((self.total * 100) 
                / tax_scale).quantize(
                    Decimal('0.01'),
                        rounding=ROUND_HALF_DOWN)
        return int(result * 100) 
Example #5
Source File: rgba.py    From ColorHelper with MIT License 5 votes vote down vote up
def round_int(dec):
    """Round float to nearest int using expected rounding."""

    return int(decimal.Decimal(dec).quantize(decimal.Decimal('0'), decimal.ROUND_HALF_DOWN)) 
Example #6
Source File: rgba.py    From sublime-markdown-popups with MIT License 5 votes vote down vote up
def round_int(dec):
    """Round float to nearest int using expected rounding."""

    return int(decimal.Decimal(dec).quantize(decimal.Decimal('0'), decimal.ROUND_HALF_DOWN)) 
Example #7
Source File: test_utils.py    From integrations-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_round_modify_method(self):
        assert round_value(3.5, rounding_method=ROUND_HALF_DOWN) == 3.0