Skip to content

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#16286
songlinn wants to merge 1 commit intoapache:masterfrom
songlinn:fix-16266

Conversation

@songlinn
Copy link
Copy Markdown

Brief Information

This pull request is in the type of:

  • bug fixing
  • new feature
  • others

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

  • The API has been changed (apache/echarts-doc#xxx).
  • This PR depends on ZRender changes (ecomfe/zrender#xxx).

Related test cases or examples to use the new APIs

N.A.

Others

Merging options

  • Please squash the commits into a single one when merging.

Other information

@echarts-bot
Copy link
Copy Markdown

echarts-bot bot commented Dec 27, 2021

Thanks for your contribution!
The community will review it ASAP. In the meanwhile, please checkout the coding standard and Wiki about How to make a pull request.

export function round(x: number | string, precision?: number, returnStr?: boolean): string | number {
if (precision == null) {
precision = 10;
precision = ROUND_SUPPORTED_PRECISION_MAX;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Author

@songlinn songlinn Dec 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
    }
`

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];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] K线图数据渲染错误

2 participants