|
1 | | -def format_large_number(number, decimals=2, concise_format=True): |
2 | | - """ |
3 | | - Converts a number in scientific notation (string format) to a concise, readable decimal string. |
| 1 | +def format_large_number(number): |
| 2 | + """Format large numbers to K, M, B, T format for both integers and floats, and handle scientific notation.""" |
| 3 | + number = float(number) |
| 4 | + |
| 5 | + if abs(number) >= 1_000_000_000_000: |
| 6 | + return f"{number/1_000_000_000_000:.2f}T" |
| 7 | + elif abs(number) >= 1_000_000_000: |
| 8 | + return f"{number/1_000_000_000:.2f}B" |
| 9 | + elif abs(number) >= 1_000_000: |
| 10 | + return f"{number/1_000_000:.2f}M" |
| 11 | + elif abs(number) >= 1_000: |
| 12 | + return f"{number/1_000:.2f}K" |
| 13 | + else: |
| 14 | + return f"{number:.2f}" |
4 | 15 |
|
5 | | - This function takes a string representing a number in scientific notation and formats it |
6 | | - into a more user-friendly, concise decimal string. It offers options for concise formatting, |
7 | | - such as removing trailing zeros and using abbreviations for large numbers (e.g., K, M, B, T). |
8 | | -
|
9 | | - Args: |
10 | | - number (str): The number in scientific notation as a string (e.g., "1.23E+06"). |
11 | | - decimals (int, optional): The number of decimal places to round to in the initial formatting. |
12 | | - Defaults to 2. Must be a non-negative integer. |
13 | | - concise_format (bool, optional): If True, applies concise formatting (removes trailing zeros and |
14 | | - uses abbreviations for large numbers). If False, returns standard |
15 | | - fixed-point formatting. Defaults to True. |
16 | | -
|
17 | | - Returns: |
18 | | - str: A string representing the number in a concise decimal format if concise_format is True, |
19 | | - or standard decimal format if False. |
20 | | - Returns "Invalid Input: Not a valid scientific number" if the input string is invalid. |
21 | | -
|
22 | | - Raises: |
23 | | - TypeError: if `number` is not a string or `decimals` is not an integer. |
24 | | - ValueError: if `decimals` is a negative integer. |
25 | | -
|
26 | | - Example: |
27 | | - >>> format_scientific_notation_concise("1.13757508810854E14") |
28 | | - '113.76T' |
29 | | - >>> format_scientific_notation_concise("1.13757508810854E14", concise_format=False) |
30 | | - '113757508810854.00' |
31 | | - >>> format_scientific_notation_concise("2.5e-3", decimals=4) |
32 | | - '0.0025' |
33 | | - >>> format_scientific_notation_concise("1234567", concise_format=True) |
34 | | - '1.23M' |
35 | | - >>> format_scientific_notation_concise("1234", concise_format=True) |
36 | | - '1.23K' |
37 | | - >>> format_scientific_notation_concise("123", concise_format=True) |
38 | | - '123' |
39 | | - """ |
40 | | - number = str(number) |
41 | | - if decimals < 0: |
42 | | - raise ValueError("Input 'decimals' must be a non-negative integer.") |
43 | | - if not isinstance(concise_format, bool): |
44 | | - raise TypeError("Input 'concise_format' must be a boolean.") |
45 | | - |
46 | | - |
47 | | - try: |
48 | | - number = float(number) |
49 | | - |
50 | | - if not concise_format: |
51 | | - return f"{number:.{decimals}f}" # Standard formatting |
52 | | - |
53 | | - # Concise formatting logic |
54 | | - magnitude = 0 |
55 | | - suffix = '' |
56 | | - if abs(number) >= 1000: |
57 | | - magnitude_suffixes = ['', 'K', 'M', 'B', 'T', 'P', 'E', 'Z', 'Y'] |
58 | | - magnitude = 0 |
59 | | - temp_number = abs(number) |
60 | | - while temp_number >= 1000 and magnitude < len(magnitude_suffixes) - 1: |
61 | | - magnitude += 1 |
62 | | - temp_number /= 1000.0 |
63 | | - suffix = magnitude_suffixes[magnitude] |
64 | | - number /= (1000**magnitude) |
65 | | - |
66 | | - |
67 | | - formatted_number = f"{number:.{decimals}f}{suffix}" |
68 | | - |
69 | | - # Remove trailing zeros after decimal point if concise format is requested |
70 | | - if concise_format and '.' in formatted_number: |
71 | | - formatted_number = formatted_number.rstrip('0').rstrip('.') if formatted_number.endswith('0') or formatted_number.endswith('.') else formatted_number |
72 | | - |
73 | | - |
74 | | - return formatted_number |
75 | | - |
76 | | - except ValueError: |
77 | | - return "Invalid Input: Not a valid scientific number" |
78 | | - |
0 commit comments