generated from Tinkoff/angular-open-source-starter
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(kit):
Number
fails to parse small number on blur (exponential n…
…otation problem) (#339)
- Loading branch information
1 parent
d1452b5
commit 7f83a7f
Showing
7 changed files
with
100 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './generate-mask-expression'; | ||
export * from './get-default-pseudo-separators'; | ||
export * from './parse-number'; | ||
export * from './stringify-number-without-exp'; |
21 changes: 21 additions & 0 deletions
21
projects/kit/src/lib/masks/number/utils/stringify-number-without-exp.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Convert number to string with replacing exponent part on decimals | ||
* | ||
* @param value the number | ||
* @return string representation of a number | ||
*/ | ||
export function stringifyNumberWithoutExp(value: number): string { | ||
const valueAsString = String(value); | ||
const [numberPart, expPart] = valueAsString.split(`e-`); | ||
|
||
let valueWithoutExp = valueAsString; | ||
|
||
if (expPart) { | ||
const [, fractionalPart] = numberPart.split(`.`); | ||
const decimalDigits = Number(expPart) + (fractionalPart?.length || 0); | ||
|
||
valueWithoutExp = value.toFixed(decimalDigits); | ||
} | ||
|
||
return valueWithoutExp; | ||
} |
39 changes: 39 additions & 0 deletions
39
projects/kit/src/lib/masks/number/utils/tests/stringify-number-without-exp.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import {stringifyNumberWithoutExp} from '../stringify-number-without-exp'; | ||
|
||
describe(`number converting to string without exponent`, () => { | ||
it(`value with exponent and without fractional part and precision > 6`, () => { | ||
expect(stringifyNumberWithoutExp(1e-10)).toBe(`0.0000000001`); | ||
}); | ||
|
||
it(`value with exponent and fractional part and precision > 6`, () => { | ||
expect(stringifyNumberWithoutExp(1.23e-8)).toBe(`0.0000000123`); | ||
}); | ||
|
||
it(`negative value with exponent and fractional part and precision > 6`, () => { | ||
expect(stringifyNumberWithoutExp(-1.23e-8)).toBe(`-0.0000000123`); | ||
}); | ||
|
||
it(`integer value`, () => { | ||
expect(stringifyNumberWithoutExp(1)).toBe(`1`); | ||
}); | ||
|
||
it(`integer value with zeros`, () => { | ||
expect(stringifyNumberWithoutExp(100)).toBe(`100`); | ||
}); | ||
|
||
it(`fractional value without exponent`, () => { | ||
expect(stringifyNumberWithoutExp(0.111)).toBe(`0.111`); | ||
}); | ||
|
||
it(`negative integer value`, () => { | ||
expect(stringifyNumberWithoutExp(-100)).toBe(`-100`); | ||
}); | ||
|
||
it(`negative fractional value`, () => { | ||
expect(stringifyNumberWithoutExp(-1e-2)).toBe(`-0.01`); | ||
}); | ||
|
||
it(`fractional value with exponent and precision equals 4`, () => { | ||
expect(stringifyNumberWithoutExp(2.23e-2)).toBe(`0.0223`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters