Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong calculation in unitToPlanck for numbers < 1 #347

Closed
Tbaut opened this issue Sep 13, 2024 · 1 comment · Fixed by #348
Closed

Wrong calculation in unitToPlanck for numbers < 1 #347

Tbaut opened this issue Sep 13, 2024 · 1 comment · Fixed by #348

Comments

@Tbaut
Copy link
Contributor

Tbaut commented Sep 13, 2024

Here 1 with 9 time 0, and a precision of 10, returns 1 with 9 times 0. Although it should return "0.1"
planckToUnit(1000000000, 10).toLocaleString('en') return 1,000,000,000 although it should return

Got some better results with the following, but I'd advise to look online for something battletested, no need to reinvent it:

export const planckToUnit = (val: bigint, units: number): number => {
  if (units < 0) {
    throw new Error(`Argument out of range: ${units}`)
  }

  const str = val.toString().padStart(units + 1, '0')
  //                               ^^^^^^^^^
  const numb = str.slice(0, -units)
  const dec = str.slice(-units)
  const result = Number(numb + '.' + dec)
  const res = !isNaN(result) ? result : 0
  return res
}
@Tbaut
Copy link
Contributor Author

Tbaut commented Sep 13, 2024

BTW toLocaleString('en') is also not helping bc it shows 0 when we get smaller than 0.00001. So I'll remove that in Delegit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant