Scientific notation
This commit is contained in:
18
hdyc-svelte/src/lib/utils/formatScientific.ts
Normal file
18
hdyc-svelte/src/lib/utils/formatScientific.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
export interface ScientificNotationParts {
|
||||
base: string;
|
||||
exponent: string;
|
||||
}
|
||||
|
||||
const SCIENTIFIC_REGEX = /^([+-]?\d+(?:\.\d+)?)(?:[eE]([+-]?\d+))$/;
|
||||
|
||||
export function parseScientificNotation(value: string): ScientificNotationParts | null {
|
||||
const match = SCIENTIFIC_REGEX.exec(value);
|
||||
if (!match) {
|
||||
return null;
|
||||
}
|
||||
const exponent = match[2].replace(/^\+/, '') || '0';
|
||||
return {
|
||||
base: match[1],
|
||||
exponent,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user