Skip to content

Improve to_str & from_str with better radix handling of integer types #4126

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
kvark opened this issue Dec 6, 2012 · 3 comments
Closed

Improve to_str & from_str with better radix handling of integer types #4126

kvark opened this issue Dec 6, 2012 · 3 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@kvark
Copy link
Contributor

kvark commented Dec 6, 2012

"float::to_str()" does not have a radix parameter, working under base 10. We should preserve this behavior for integer types as well. We also need "to_str_radix()" and "from_str_radix()" functions for integer types.

@therealgymmy
Copy link
Contributor

Agreed, the second parameter to float::to_str() specifies the number of significant digits, which is not consistent with integer types. (Before looking at float.rs, I thought it would take in a parameter specifying base just as in integer types and was quite puzzled by the result I got...)

to_str_radix can be added for now just by adding an additional trait for int. Although I don't think making another trait just to enable this feature is a good idea, it's probably the quickest fix.
You can just add the following code to either the rust lib code, or in one of your own files.

trait ToStrRadix { pure fn to_str_radix (radix: uint) -> str; }
impl int: ToStrRadix {
    pure fn to_str_radix (radix: uint) -> str { int::to_str(self, radix) }
}
// do the same for other types that support radix

@Kimundi
Copy link
Member

Kimundi commented Dec 25, 2012

Just a heads-up that I'm working on a unified interface for uint, int and float for that stuff right now.

@Kimundi
Copy link
Member

Kimundi commented Feb 27, 2013

This can be closed now, I think.

@graydon graydon closed this as completed Feb 27, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

4 participants