Skip to content

radix in toString #586

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

Closed
bowenwang1996 opened this issue May 10, 2019 · 8 comments
Closed

radix in toString #586

bowenwang1996 opened this issue May 10, 2019 · 8 comments

Comments

@bowenwang1996
Copy link
Contributor

Is there plan to support radix in toString for integer types?

@dcodeIO
Copy link
Member

dcodeIO commented May 10, 2019

Of course, yeah, is on todo but afaik nobody is currently working on it. Code responsible for this and similar things lives in std/internal/number.

@MaxGraey
Copy link
Member

But this change little bit with upcoming rework runtime (#535) branch

@stale
Copy link

stale bot commented Jun 9, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jun 9, 2019
@stale stale bot closed this as completed Jun 16, 2019
@darky
Copy link

darky commented Mar 18, 2020

Is any workaround exists for it?

@letmaik
Copy link

letmaik commented Oct 11, 2020

This is working now, but if you wrap it in a function then the type must not be number but one of the explicit ones like u8. I think this is a bug.

function hex(n: number): string {
  return '0x' + n.toString(16)
}

@MaxGraey
Copy link
Member

MaxGraey commented Oct 11, 2020

number in AssemblyScript is just alias of f64. We can't implement conversions from float to string in hexadecimal format because it's unusable. For example in JS: (1.2345e10).toString(16) == "2dfd1c040". It's not hexadecimal float points (in this case we would have seen 0x1.6fe8e02p+33), it's something very legacy which we try to avoid

@letmaik
Copy link

letmaik commented Oct 11, 2020

I see, I guess this is the correct approach:

function hex<T>(n: T): string {
  return '0x' + n.toString(16)
}

This compiles and works fine, although I get a typescript error in the editor: "Property 'toString' does not exist on type 'T'."

@MaxGraey
Copy link
Member

function hex<T extends number>(n: T): string {
  return '0x' + n.toString(16);
}

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

No branches or pull requests

5 participants