fix: change the precision of number to Number.toFixed max precision#16286
Closed
songlinn wants to merge 1 commit intoapache:masterfrom
songlinn:fix-16266
Closed
fix: change the precision of number to Number.toFixed max precision#16286songlinn wants to merge 1 commit intoapache:masterfrom songlinn:fix-16266
songlinn wants to merge 1 commit intoapache:masterfrom
songlinn:fix-16266
Conversation
|
Thanks for your contribution! |
pissang
requested changes
Dec 27, 2021
src/util/number.ts
Outdated
| export function round(x: number | string, precision?: number, returnStr?: boolean): string | number { | ||
| if (precision == null) { | ||
| precision = 10; | ||
| precision = ROUND_SUPPORTED_PRECISION_MAX; |
Contributor
There was a problem hiding this comment.
ROUND_SUPPORTED_PRECISION_MAX is a safe protection for toFixed methods. The actual precision issue in the JS engine is less than this.
Check this in your console
(0.1 + 0.2).toFixed(20);
Author
There was a problem hiding this comment.
What do you think auto calculate precision by number decimal place length?
such as :
if (precision == null) {
const exp = String(x).split('e')[1];
precision = Number(-exp) || 10;
}
`
pissang
requested changes
Dec 28, 2021
| if (precision == null) { | ||
| precision = 10; | ||
| // auto adjust round precision by decimal place length only when the number is using scientific | ||
| const exp = String(x).split('e')[1]; |
Contributor
There was a problem hiding this comment.
It's not the precision x. 1.3e-10 will have exp 10 here. But it should be 11.
Also we prefer x + '' to convert a value to string. And -exp already convert it to a number.
Closed
8 tasks
This pull request was closed.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Brief Information
This pull request is in the type of:
What does this PR do?
fix number default precision
Fixed issues
#16266
Details
Before: What was the problem?
After: How is it fixed in this PR?
At issues, the dataset is
['0.000000000012', '0.000000000034', '0.000000000010', '0.000000000038'], but the number default precision is 10 ,value " 0.000000000012" will be round to 0. therefore i change the default precision to the constant value ROUND_SUPPORTED_PRECISION_MAX.Misc
Related test cases or examples to use the new APIs
N.A.
Others
Merging options
Other information