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

Floats incorrectly serialized to xml #1284

Closed
peterlozano opened this issue Feb 15, 2021 · 6 comments
Closed

Floats incorrectly serialized to xml #1284

peterlozano opened this issue Feb 15, 2021 · 6 comments

Comments

@peterlozano
Copy link

After an upgrade from 1.14.1 to 3.11.0, float values were not serialized correctly.

For example:
348.47 was serialized to xml as 348.46999999999997

I'm using PHP 7.2.23

It seems var_export may format floats this way for some reason:

return $this->document->createTextNode(var_export((float) $data, true));

See:

@zeldi-dev
Copy link

I am experiencing the same issue after upgrading to v3.
Is there any workaround or plan to change the behaviour?

@goetas
Copy link
Collaborator

goetas commented Apr 25, 2021

an option would be to introduce a "precision" parameter to float numbers.... something linke float<1> could serialize 348.47 into 348.5

@machinateur
Copy link

machinateur commented Jul 21, 2021

Hello everyone,

I've experienced the same problem earlier today and could not find any information on how to format float values in the docs or elsewhere. I ended up using round() to fix it (hopefully, I haven't been able to test it properly).

Also, I'm unable to reproduce the results described by @peterlozano on 3v4l cutting the middleman and directly giving the float to var_export(). So I can't even reproduce the behavior I was getting myself... Am I missing something?

Best regards

@goetas
Copy link
Collaborator

goetas commented Jul 26, 2021

an option would be to introduce a "precision" parameter to float numbers.... something linke float<1> could serialize 348.47 into 348.5

it would be great if someone could send a PR with such improvement

@re2bit
Copy link

re2bit commented Oct 28, 2021

And here it is ;)

@goetas
Copy link
Collaborator

goetas commented Oct 30, 2021

done in #1359

@goetas goetas closed this as completed Oct 30, 2021
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

No branches or pull requests

5 participants