# 10 examples of 'python format float 2 decimal' in Python

Every line of 'python format float 2 decimal' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

## All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
``16def format_decimal(value):17    return Decimal(value.quantize(Decimal('.01'), rounding=ROUND_HALF_UP))``
``24def float_str(f, min_digits=2, max_digits=6):25    """26    Returns a string representing a float, where the number of27    significant digits is min_digits unless it takes more digits28    to hit a non-zero digit (and the number is 0 &lt; x &lt; 1).29    We stop looking for a non-zero digit after max_digits.30    """31    if f &gt;= 1 or f &lt;= 0:32        return str(round_float(f, min_digits))33    start_str = str(round_float(f, max_digits))34    digits = start_str.split(".")[1]35    non_zero_indices = []36    for i, digit in enumerate(digits):37        if digit != "0":38            non_zero_indices.append(i + 1)39    # Only saw 0s.40    if len(non_zero_indices) == 0:41        num_digits = min_digits42    else:43        # Of the non-zero digits, pick the num_digit'th of those (including any zeros)44        min_non_zero_indices = range(non_zero_indices[0], non_zero_indices[-1] + 1)[:min_digits]45        num_digits = min_non_zero_indices[-1]46    return str(round_float(f, num_digits))``
``48def format_float(x):49    try:50        return "%10.5f" % x51    except:52        return str(x)``
``114def decimal2text(value, places=2,115                 int_units=(('', '', ''), 'm'),116                 exp_units=(('', '', ''), 'm')):117    value = decimal.Decimal(value)118    q = decimal.Decimal(10) ** -places119120    integral, exp = str(value.quantize(q)).split('.')121    return u'{} {}'.format(122        num2text(int(integral), int_units),123        num2text(int(exp), exp_units))``
``5def fmtFloat(n):6    return '{:6.3f}'.format(n)``
``55def representacion(float, format=0, total=0, decimales=4, exp=False, tol=5,56                   signo=False, thousand=False):57    """Function for string representation of float values58    float: number to transform59    format: mode60        0   -   fixed point61        1   -   Significant figures62        2   -   Engineering format63    total: total number of digits64    decimales: decimal number65    exp: boolean to use engineering repr for big or small number66    tol: exponent limit tu use engineering repr over float normal repr67    signo: show sign68    thousand: use thousan separator point69    """70    if type(float) is str:71        return float7273    if signo:74        start = "{:+"75    else:76        start = "{: "7778    if thousand:79        coma = ",."80    else:81        coma = "."8283    if exp and (-10**tol &gt; float or (-10**-tol &lt; float &lt; 10**(-tol+1) and84                                     float != 0) or float &gt; 10**tol):85        format = 286    if float == 0:87        decimales = 18889    if format == 1:90        string = start+"{}{:d}g".format(coma, decimales)+"}"91    elif format == 2:92        string = start+"{:d}{}{:d}e".format(total, coma, decimales)+"}"93    else:94        string = start+"{:d}{}{:d}f".format(total, coma, decimales)+"}"9596    return string.format(float)``
``60def test_decimal_int(self):61    a, b = mathfilters.handle_float_decimal_combinations(Decimal('2.0'), 1, '+')62    self.assertTrue(isinstance(a, Decimal), 'Type is {0}'.format(type(a)))63    self.assertTrue(isinstance(b, int), 'Type is {0}'.format(type(b)))``
``66def test_float(self):67    self.assertEqual(sqlrepr(10.01), "10.01")``
``238def test_as_decimal():239    amount = EUR('1.23')240    quantized_decimal = Decimal('1.23').quantize(Decimal('.01'))241    assert amount.as_decimal() == quantized_decimal``
``4def format_float_text(value):5    v = value6    if isinstance(v, u.Quantity):7        v = value.value8    elif isinstance(v, str):9        v = float(value)1011    if 0.001 &lt;= abs(v) &lt;= 1000 or abs(v) == 0.0:12        return "{0:.3f}".format(value)13    else:14        return "{0:.3e}".format(value)``