当前位置: 首页>>代码示例>>Python>>正文


Python decimal.Rounded方法代码示例

本文整理汇总了Python中decimal.Rounded方法的典型用法代码示例。如果您正苦于以下问题:Python decimal.Rounded方法的具体用法?Python decimal.Rounded怎么用?Python decimal.Rounded使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在decimal的用法示例。


在下文中一共展示了decimal.Rounded方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: format_number

# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import Rounded [as 别名]
def format_number(value, max_digits, decimal_places):
    """
    Formats a number into a string with the requisite number of digits and
    decimal places.
    """
    if value is None:
        return None
    if isinstance(value, decimal.Decimal):
        context = decimal.getcontext().copy()
        if max_digits is not None:
            context.prec = max_digits
        if decimal_places is not None:
            value = value.quantize(decimal.Decimal(".1") ** decimal_places, context=context)
        else:
            context.traps[decimal.Rounded] = 1
            value = context.create_decimal(value)
        return "{:f}".format(value)
    if decimal_places is not None:
        return "%.*f" % (decimal_places, value)
    return "{:f}".format(value) 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:22,代码来源:utils.py

示例2: format_number

# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import Rounded [as 别名]
def format_number(value, max_digits, decimal_places):
    """
    Format a number into a string with the requisite number of digits and
    decimal places.
    """
    if value is None:
        return None
    if isinstance(value, decimal.Decimal):
        context = decimal.getcontext().copy()
        if max_digits is not None:
            context.prec = max_digits
        if decimal_places is not None:
            value = value.quantize(decimal.Decimal(".1") ** decimal_places, context=context)
        else:
            context.traps[decimal.Rounded] = 1
            value = context.create_decimal(value)
        return "{:f}".format(value)
    if decimal_places is not None:
        return "%.*f" % (decimal_places, value)
    return "{:f}".format(value) 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:22,代码来源:utils.py

示例3: format_number

# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import Rounded [as 别名]
def format_number(value, max_digits, decimal_places):
    """
    Format a number into a string with the requisite number of digits and
    decimal places.
    """
    if value is None:
        return None
    if isinstance(value, decimal.Decimal):
        context = decimal.getcontext().copy()
        if max_digits is not None:
            context.prec = max_digits
        if decimal_places is not None:
            value = value.quantize(decimal.Decimal(1).scaleb(-decimal_places), context=context)
        else:
            context.traps[decimal.Rounded] = 1
            value = context.create_decimal(value)
        return "{:f}".format(value)
    if decimal_places is not None:
        return "%.*f" % (decimal_places, value)
    return "{:f}".format(value) 
开发者ID:PacktPublishing,项目名称:Hands-On-Application-Development-with-PyCharm,代码行数:22,代码来源:utils.py

示例4: test_format_number

# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import Rounded [as 别名]
def test_format_number(self):
        def equal(value, max_d, places, result):
            self.assertEqual(format_number(Decimal(value), max_d, places), result)

        equal('0', 12, 3, '0.000')
        equal('0', 12, 8, '0.00000000')
        equal('1', 12, 9, '1.000000000')
        equal('0.00000000', 12, 8, '0.00000000')
        equal('0.000000004', 12, 8, '0.00000000')
        equal('0.000000008', 12, 8, '0.00000001')
        equal('0.000000000000000000999', 10, 8, '0.00000000')
        equal('0.1234567890', 12, 10, '0.1234567890')
        equal('0.1234567890', 12, 9, '0.123456789')
        equal('0.1234567890', 12, 8, '0.12345679')
        equal('0.1234567890', 12, 5, '0.12346')
        equal('0.1234567890', 12, 3, '0.123')
        equal('0.1234567890', 12, 1, '0.1')
        equal('0.1234567890', 12, 0, '0')
        equal('0.1234567890', None, 0, '0')
        equal('1234567890.1234567890', None, 0, '1234567890')
        equal('1234567890.1234567890', None, 2, '1234567890.12')
        equal('0.1234', 5, None, '0.1234')
        equal('123.12', 5, None, '123.12')

        with self.assertRaises(Rounded):
            equal('0.1234567890', 5, None, '0.12346')
        with self.assertRaises(Rounded):
            equal('1234567890.1234', 5, None, '1234600000') 
开发者ID:nesdis,项目名称:djongo,代码行数:30,代码来源:test_utils.py

示例5: test_format_number

# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import Rounded [as 别名]
def test_format_number(self):
        """
        Test the format_number converter utility
        """
        def equal(value, max_d, places, result):
            self.assertEqual(format_number(Decimal(value), max_d, places), result)

        equal('0', 12, 3,
              '0.000')
        equal('0', 12, 8,
              '0.00000000')
        equal('1', 12, 9,
              '1.000000000')
        equal('0.00000000', 12, 8,
              '0.00000000')
        equal('0.000000004', 12, 8,
              '0.00000000')
        equal('0.000000008', 12, 8,
              '0.00000001')
        equal('0.000000000000000000999', 10, 8,
              '0.00000000')
        equal('0.1234567890', 12, 10,
              '0.1234567890')
        equal('0.1234567890', 12, 9,
              '0.123456789')
        equal('0.1234567890', 12, 8,
              '0.12345679')
        equal('0.1234567890', 12, 5,
              '0.12346')
        equal('0.1234567890', 12, 3,
              '0.123')
        equal('0.1234567890', 12, 1,
              '0.1')
        equal('0.1234567890', 12, 0,
              '0')
        equal('0.1234567890', None, 0,
              '0')
        equal('1234567890.1234567890', None, 0,
              '1234567890')
        equal('1234567890.1234567890', None, 2,
              '1234567890.12')
        equal('0.1234', 5, None,
              '0.1234')
        equal('123.12', 5, None,
              '123.12')
        with self.assertRaises(Rounded):
            equal('0.1234567890', 5, None,
                  '0.12346')
        with self.assertRaises(Rounded):
            equal('1234567890.1234', 5, None,
                  '1234600000') 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:53,代码来源:tests.py


注:本文中的decimal.Rounded方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。