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

Output of Float Numbers - Rounding Not Enforced #184

Open
npruzaniec opened this issue Oct 5, 2023 · 2 comments
Open

Output of Float Numbers - Rounding Not Enforced #184

npruzaniec opened this issue Oct 5, 2023 · 2 comments
Labels

Comments

@npruzaniec
Copy link

Description

I am using element-api to output JSON from Craft number fields (which are themselves limited to 1 or 2 decimal places) but for some reason, on output these are extended to 15 decimal places regardless of any PHP rounding rules I use in my controller classes.

Steps to reproduce

  1. Output number field in an API endpoint.
  2. View that output in the browser.

Additional info

  • Craft version: Craft Pro 4.5.6.1
  • PHP version: 8.0.28
  • Database driver & version: MariaDB 10.4.28
  • Plugins & versions:
    Screenshot 2023-10-05 at 10 50 00 AM

Code:

return [
  'imperial' => [
    'value' => round($imperial, 2),
    'units' => $units->unit_label->imperial,
  ],
  'metric' => [
    'value' =>  round($metric, 2),
    'units' => $units->unit_label->metric,
  ]
];

Output:

"imperial": {
  "value": 10.800000000000001,
  "units": "in"
},
 "metric": {
  "value": 273,
  "units": "mm"
}
@npruzaniec npruzaniec added the bug label Oct 5, 2023
@npruzaniec
Copy link
Author

I should add, the output is the same with or without the round() function.

@npruzaniec
Copy link
Author

I found a server configuration that solved this for me after I discovered the issue did not exist when deployed to our staging server.

I am running this locally via DDEV. Adding a php.ini file to /craft/.ddev/php/php.ini with the following contents resolved the float precision issue for me:

[PHP]
serialize_precision = -1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant