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

Parsing very very tiny float. #494

Closed
wants to merge 1 commit into from
Closed

Conversation

gmponos
Copy link
Contributor

@gmponos gmponos commented Aug 18, 2018

Hello there....

I have an external API that executes some calculations and returns a result as a report. One of these results happened to be an amount that was a very very tiny one. 2.8865798640254e-15

My code that consumes the API parses this value to a Money object automatically by doing:

$money = $parser->parse((string)$amount, $currency);

I know that there is no actual currency that could represent a so tiny amount and it is practically zero (maybe in some bitcoins it have some value).

So the code above throws exception because the value is converted to a string in this form: 2.8865798640254e-15 and this does not pass the expression inside the DecimalParser. For now in order to stop getting exception I had to do a quick solution to round the number to 7 decimals and then pass it inside the parser and it feels kinda hackish.

As a better solution I could create something like a ExponentialParser and pass it inside an AggregateParser but before going into that I felt that I should ask if you believe that this should be handled by the DecimalParser and not by a different parser...

If yes maybe I could implement the changes on this PR.

For my case above I have added a test case to check it.

@frederikbosch
Copy link
Member

@gmponos I have some questions before coming to a solution.

  1. What does that API return: a string with lots of fractional numbers or a really exponential floating point?
  2. How is that number represented in PHP: a float or a string?
  3. If it is a float, is it bigger than the constant PHP_FLOAT_MAX (constant available since PHP 7.2)?

And maybe you can try this one in the meanwhile.

use Money\Number;
$money = $parser->parse((string)Number::fromNumber($amount), $currency);

@gmponos
Copy link
Contributor Author

gmponos commented Aug 20, 2018

Hello, basically most of the questions can be answered through the test case that I have added.

On the test case I just simulated the same thing as my App is doing but let's take it from the top and answer your questions just to be specific here:

  1. It returns it as exponential.
  2. it is represented as a float. check the test.
  3. I guess not because PHP_FLOAT_MIN is 2.2250738585072e-308 and I just have e-15

I will give a try to the Number but never the less should this be inside the DecimalParser?

@sagikazarmark sagikazarmark added this to the 3.3.0 milestone Sep 12, 2018
@gmponos
Copy link
Contributor Author

gmponos commented Sep 25, 2018

Any idea how this should be handled after all?

@frederikbosch
Copy link
Member

Closed in favour of #520.

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 this pull request may close these issues.

3 participants