{"action":"created","issue":{"url":"https://api.github.com/repos/ericmj/decimal/issues/18","labels_url":"https://api.github.com/repos/ericmj/decimal/issues/18/labels{/name}","comments_url":"https://api.github.com/repos/ericmj/decimal/issues/18/comments","events_url":"https://api.github.com/repos/ericmj/decimal/issues/18/events","html_url":"https://github.com/ericmj/decimal/pull/18","id":113143373,"number":18,"title":"Implements to_integer and to_float","user":{"login":"solatis","id":119317,"avatar_url":"https://avatars.githubusercontent.com/u/119317?v=3","gravatar_id":"","url":"https://api.github.com/users/solatis","html_url":"https://github.com/solatis","followers_url":"https://api.github.com/users/solatis/followers","following_url":"https://api.github.com/users/solatis/following{/other_user}","gists_url":"https://api.github.com/users/solatis/gists{/gist_id}","starred_url":"https://api.github.com/users/solatis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/solatis/subscriptions","organizations_url":"https://api.github.com/users/solatis/orgs","repos_url":"https://api.github.com/users/solatis/repos","events_url":"https://api.github.com/users/solatis/events{/privacy}","received_events_url":"https://api.github.com/users/solatis/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":13,"created_at":"2015-10-24T07:22:17Z","updated_at":"2015-10-26T15:10:42Z","closed_at":"2015-10-25T14:58:36Z","pull_request":{"url":"https://api.github.com/repos/ericmj/decimal/pulls/18","html_url":"https://github.com/ericmj/decimal/pull/18","diff_url":"https://github.com/ericmj/decimal/pull/18.diff","patch_url":"https://github.com/ericmj/decimal/pull/18.patch"},"body":"The Decimal library allows construction of %Decimal{} using either a string, a float or an int. If you ever want to get a regular number out of the Decimal again, you need to use to_string and then parse the string. \r\n\r\nWhen handling monetary values, you want to represent the value while working with it as a Decimal, but might need to store it in a database. For example, for USD, you would do something like this:\r\n\r\n```elixir\r\nusd = 12.34\r\ncents = round(usd * :math.pow(10, 2))\r\nnum = %Decimal{sign: 1, coef: cents, exp: -2)\r\n\r\n# do stuff with num\r\n\r\ncents = Decimal.mult(num, %Decimal{sign: 1, coef: 1, exp: 2})\r\n\r\n# store cents in db\r\n```\r\n\r\nAt this point, you just want to get the integer value of `cents`. This commit provides two functions: `to_integer/1` and `to_float/1`. Both of these functions can have loss of precision if used incorrectly, but should be safe when used correctly. Perhaps it is a good idea to provide an `integer?` function to test whether the Decimal can safely be converted to an integer?\r\n\r\nTest cases are also provided.\r\n"},"comment":{"url":"https://api.github.com/repos/ericmj/decimal/issues/comments/151168532","html_url":"https://github.com/ericmj/decimal/pull/18#issuecomment-151168532","issue_url":"https://api.github.com/repos/ericmj/decimal/issues/18","id":151168532,"user":{"login":"ericmj","id":316890,"avatar_url":"https://avatars.githubusercontent.com/u/316890?v=3","gravatar_id":"","url":"https://api.github.com/users/ericmj","html_url":"https://github.com/ericmj","followers_url":"https://api.github.com/users/ericmj/followers","following_url":"https://api.github.com/users/ericmj/following{/other_user}","gists_url":"https://api.github.com/users/ericmj/gists{/gist_id}","starred_url":"https://api.github.com/users/ericmj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ericmj/subscriptions","organizations_url":"https://api.github.com/users/ericmj/orgs","repos_url":"https://api.github.com/users/ericmj/repos","events_url":"https://api.github.com/users/ericmj/events{/privacy}","received_events_url":"https://api.github.com/users/ericmj/received_events","type":"User","site_admin":false},"created_at":"2015-10-26T15:10:42Z","updated_at":"2015-10-26T15:10:42Z","body":"I think you wrote this in the wrong issue :)"}}