File tree 2 files changed +24
-2
lines changed
2 files changed +24
-2
lines changed Original file line number Diff line number Diff line change 1
- import type { FormatOptions , Numberish } from './types'
1
+ import { rtrim } from '../string'
2
+ import type { FormatOptions , NumberString , Numberish } from './types'
3
+
4
+ export function convertExponentialToString ( num : Numberish ) {
5
+ const str = num . toString ( )
6
+
7
+ if ( ! str . includes ( 'e' ) ) {
8
+ return str as NumberString
9
+ }
10
+
11
+ if ( ! str . includes ( 'e-' ) ) {
12
+ return BigInt ( num ) . toString ( ) as NumberString
13
+ }
14
+
15
+ const [ base , exp ] = str . split ( 'e-' ) . map ( Number )
16
+ const precision = base . toString ( ) . replace ( '.' , '' ) . length + exp
17
+
18
+ return rtrim ( Number . parseFloat ( str ) . toFixed ( precision ) , '0' ) as NumberString
19
+ }
2
20
3
21
export function format ( number : Numberish , options : FormatOptions = { } ) {
4
22
let leadingZerosCount = 0
5
23
let { maximumFractionDigits = 4 , groupFractionLeadingZeros = false } = options
6
24
25
+ number = convertExponentialToString ( number )
26
+
7
27
const { exactFractionWhenZero = true , maximumFractionLeadingZeros = maximumFractionDigits } = options
8
28
const [ integerPart , fractionPart = '0' ] = String ( number ) . split ( '.' )
9
29
Original file line number Diff line number Diff line change 1
- export type Numberish = `${number } ` | 'Infinity' | '-Infinity' | '+Infinity' | number | bigint
1
+ export type NumberString = `${number } ` | 'Infinity' | '-Infinity' | '+Infinity'
2
+
3
+ export type Numberish = NumberString | number | bigint
2
4
3
5
export interface FormatOptions extends Intl . NumberFormatOptions {
4
6
locales ?: string | string [ ]
You can’t perform that action at this time.
0 commit comments