MCPcopy Index your code
hub / github.com/pyfa-org/Pyfa / formatAmount

Function formatAmount

gui/utils/numberFormatter.py:6–101  ·  view source on GitHub ↗

Add suffix to value, transform value to match new suffix and round it. Keyword arguments: val -- value to process prec -- precision of final number (number of significant positions to show) lowest -- lowest order for suffixizing for numbers 0 < |num| < 1 highest -- h

(val, prec=3, lowest=0, highest=0, currency=False, forceSign=False, unitName=None)

Source from the content-addressed store, hash-verified

4
5
6def formatAmount(val, prec=3, lowest=0, highest=0, currency=False, forceSign=False, unitName=None):
7 """
8 Add suffix to value, transform value to match new suffix and round it.
9
10 Keyword arguments:
11 val -- value to process
12 prec -- precision of final number (number of significant positions to show)
13 lowest -- lowest order for suffixizing for numbers 0 < |num| < 1
14 highest -- highest order for suffixizing for numbers |num| > 1
15 currency -- if currency, billion suffix will be B instead of G
16 forceSign -- if True, positive numbers are signed too
17 unitName -- if specified, will be formatted into a string
18 """
19 if val is None:
20 return ""
21 if val == math.inf:
22 return "\u221e" if unitName is None else "\u221e {}".format(unitName)
23 # Define suffix maps
24 posSuffixMap = {3: "k", 6: "M", 9: "B" if currency is True else "G"}
25 negSuffixMap = {-6: '\u03bc', -3: "m"}
26 # Define tuple of the map keys
27 # As we're going to go from the biggest order of abs(key), sort
28 # them differently due to one set of values being negative
29 # and other positive
30 posOrders = tuple(sorted(iter(posSuffixMap.keys()), reverse=True))
31 negOrders = tuple(sorted(iter(negSuffixMap.keys()), reverse=False))
32 # Find the least abs(key)
33 posLowest = min(posOrders)
34 negHighest = max(negOrders)
35 # By default, mantissa takes just value and no suffix
36 mantissa, suffix = val, ""
37 # Positive suffixes
38 if abs(val) > 1 and highest >= posLowest:
39 # Start from highest possible suffix
40 for key in posOrders:
41 # Find first suitable suffix and check if it's not above highest order
42 if abs(val) >= 10 ** key and key <= highest:
43 mantissa, suffix = val / float(10 ** key), posSuffixMap[key]
44 # Do additional step to eliminate results like 999999 => 1000k
45 # If we're already using our greatest order, we can't do anything useful
46 if posOrders.index(key) == 0:
47 break
48 else:
49 # Get order greater than current
50 prevKey = posOrders[posOrders.index(key) - 1]
51 # Check if the key to which we potentially can change is greater
52 # than our highest boundary
53 if prevKey > highest:
54 # If it is, bail - we already have acceptable results
55 break
56 # Find multiplier to get from one order to another
57 orderDiff = 10 ** (prevKey - key)
58 # If rounded mantissa according to our specifications is greater than
59 # or equal to multiplier
60 if roundToPrec(mantissa, prec) >= orderDiff:
61 # Divide mantissa and use suffix of greater order
62 mantissa, suffix = mantissa / orderDiff, posSuffixMap[prevKey]
63 # Otherwise consider current results as acceptable

Callers 15

firepowerSectionFunction · 0.90
tankSectionFunction · 0.90
repsSectionFunction · 0.90
miscSectionFunction · 0.90
__getDataMethod · 0.90
getTextMethod · 0.90
getTextMethod · 0.90
getTextMethod · 0.90
getTextMethod · 0.90
getTextMethod · 0.90
getTextMethod · 0.90
getToolTipMethod · 0.90

Calls 2

roundToPrecFunction · 0.90
keysMethod · 0.45

Tested by

no test coverage detected