|
| 1 | +**@haqq/format-number-with-subscript-zeros v1.0.0** • [**Docs**](globals.md) |
| 2 | + |
| 3 | +*** |
| 4 | + |
| 5 | +# Format Number with Subscript Zeros |
| 6 | + |
| 7 | +A library to format numbers by hiding leading zeros using subscript digits. This method is particularly useful for representing very small numbers in a compact and readable format. The subscript digits indicate the number of hidden zeros. |
| 8 | + |
| 9 | +[Documentation](https://github.com/haqq-network/format-number-with-subscript-zeros/blob/master/docs/globals.md) |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +```bash |
| 14 | +yarn add @haqq/format-number-with-subscript-zeros |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +```ts |
| 20 | +import { formatNumberWithSubscriptZeros } from '@haqq/format-number-with-subscript-zeros'; |
| 21 | + |
| 22 | +console.log(formatNumberWithSubscriptZeros("0.42")); // "0.42" |
| 23 | +console.log(formatNumberWithSubscriptZeros("0.042")); // "0.042" |
| 24 | +console.log(formatNumberWithSubscriptZeros("0.0042")); // "0.0042" |
| 25 | +console.log(formatNumberWithSubscriptZeros("0.00042")); // "0.00042" |
| 26 | +console.log(formatNumberWithSubscriptZeros("0.000042")); // "0.000042" |
| 27 | +console.log(formatNumberWithSubscriptZeros("0.0000042")); // "0.0₅42" |
| 28 | +console.log(formatNumberWithSubscriptZeros("0.00000042")); // "0.0₆42" |
| 29 | +console.log(formatNumberWithSubscriptZeros("0.000000042")); // "0.0₇42" |
| 30 | +console.log(formatNumberWithSubscriptZeros("0.0000000042")); // "0.0₈42" |
| 31 | +console.log(formatNumberWithSubscriptZeros("0.00000000042")); // "0.0₉42" |
| 32 | +console.log(formatNumberWithSubscriptZeros("0.000000000042")); // "0.0₁₀42" |
| 33 | +console.log(formatNumberWithSubscriptZeros("0.0000000000042")); // "0.0₁₁42" |
| 34 | +console.log(formatNumberWithSubscriptZeros("0.00000000000042")); // "0.0₁₂42" |
| 35 | +``` |
| 36 | + |
| 37 | +### Additional Parameters |
| 38 | + |
| 39 | +The formatNumberWithSubscriptZeros function accepts the following parameters: |
| 40 | + |
| 41 | + - numberStr (string): The number string to format. |
| 42 | + - presiction (number, optional): The number of decimal places to include in the formatted string. Default is 3. |
| 43 | + - min (number, optional): The minimum value for which the formatting should be applied. Default is 0.00001. |
| 44 | + |
| 45 | +## Running Tests |
| 46 | + |
| 47 | +```sh |
| 48 | +yarn test |
| 49 | +``` |
| 50 | + |
| 51 | +## Building the Project |
| 52 | + |
| 53 | +```sh |
| 54 | +yarn build |
| 55 | +``` |
| 56 | + |
| 57 | +## Linting the Code |
| 58 | + |
| 59 | +```sh |
| 60 | +yarn lint |
| 61 | +``` |
| 62 | + |
| 63 | +## Benefits and Convenience |
| 64 | + |
| 65 | +This method of formatting numbers is particularly useful for: |
| 66 | + |
| 67 | + • Readability: It makes very small numbers easier to read and understand by representing leading zeros as subscript digits. |
| 68 | + • Compact Representation: It reduces the length of the number string by using subscript digits for leading zeros. |
| 69 | + • Scientific Applications: It is useful in scientific and engineering contexts where numbers can be extremely small and a clear representation is needed. |
| 70 | + |
| 71 | +## How It Works |
| 72 | + |
| 73 | +The function converts the number string to a floating-point number and checks if it is less than 0.01. If the number is larger, it returns the original string. For smaller numbers, it calculates the number of leading zeros and formats them as subscript digits. |
| 74 | + |
| 75 | +## License |
| 76 | + |
| 77 | +MIT |
0 commit comments