- Notifications
You must be signed in to change notification settings - Fork 3.5k
String.to_integer and String.to_float #1044
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
String.to_integer and String.to_float #1044
Conversation
| Are those unrelated commits? |
| Yes, I did a pull request earlier that got rejected [the h(Module...)], and the merge when I did the latest pull. Sorry about that. Should I delete this request, do a rebase, and resubmit the request? |
| You can rebase and force push. |
| Unfortunately, I have no idea what that involves in terms of git commands.(Git is a "know everything before doing anything" system.) I did a rebase on the branch, and it just said "noop" in the file that it generated. |
| Assuming master is updated to latest upstream, do the following: git checkout master git branch -M string_to_int_float old git checkout -b string_to_int_float git cherry-pick old git push -f |
| OK; thanks. I feel like I have just chanted a mystical voodoo spell; I hope the repository doesn't die a horrible death as a result :) |
| Still nope, looks like master wasn't clean, make sure it doesn't have those commits. |
| In that case, I think my best course of action is to find some way to delete this request, destroy my fork of elixir, create a new one, and start all over. |
| It cannot be destroyed, only closed and a new one opened. Assuming git checkout master git fetch upstream git reset --hard upstream/master git branch -D string_to_int_float git checkout -b string_to_int_float git cherry-pick old git push -f |
| OK; I did that. If it didn't work, then I'll just close this request, delete my fork, and I promise to never, ever again make more trouble by issuing a pull request to this or any other project. |
Given a string that starts with a valid integer, String.to_integer/1 returns a tuple {result, remainder} where remainder is the character string following the valid integer. If the input is not valid as specified by Erlang's :string.to_integer/1, the function returns :error. Given a string that starts with a valid float, String.to_float/1 returns a tuple {result, remainder} where remainder is the character string following the valid float. If the input is not valid as specified by Erlang's :string.to_float/1, the function then calls String.to_integer/1 and returns that value, which is either an {integer result, remainder} or :error. This latter behavior allows you to say String.to_float("34") without generating an error. | It worked :) /me is a git wizard |
| Thank you! You saved me at least the five hours it would have taken me to figure it out. |
| Isn't it generally against Elixir policy to simply wrap Erlang APIs? What does this give us over |
| One thing I don't like is that String.to_float can return an integer when you asked it to return a float. This could potentially break somebody expecting the return value to be a float. |
| Devinus: I implemented the results of the discussion at #1029, where there appeared to be agreement that it might be useful. |
| @jdeisenberg Okay, can we at least ensure String.to_float returns a float? |
Added code to make String.to_float("34") return {34.0, ""} | Thanks @jdeisenberg! The pull request looks good but we need tests before we can merge this. After this is merged, we should probably break it apart into our own implementation (using pattern matching on binaries). |
| @jdeisenberg @josevalim Let me make an example of why String.to_float should always return floats: float1 = String.to_float("12,0") float2 = String.to_float("12.0") float1 === float |
| @josevalim: I'll write some tests tomorrow (right now while I am tired would be a really bad idea); @devinus: I've already put in that patch. |
| @jdeisenberg awesome @josevalim regarding writing it our own, does :string do this as nifs? |
| Tests added (see comment above). |
String.to_integer and String.to_float
| OK, I pushed that change on the current branch, but am not sure how to re-activate the pull request, unless I have to start yet another branch. In which case I am stuck, because I can't undo anything in git. |
| You have to make a new pull request after one has been merged. |
Implement String.to_integer/1 and String.to_float/1. A successful conversion returns a tuple in the form {value, remainder} where remainder is the part of the string following the valid number. An unsuccessful conversion returns :error.
Unlike Erlang's :string.to_float/1 function, String.to_float/1 will accept an integer as valid and return it as an integer value.