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

Possibility to remove trailing zeros on DecimalFields representation #6514

Merged
merged 5 commits into from
Nov 16, 2022

Conversation

Krolken
Copy link
Contributor

@Krolken Krolken commented Mar 19, 2019

Description

It would be good to have a way of remove trailing zeros in output. For example when working with a lot of decimal precision you now get very long strings when representing them as string in outputs.

# Current
Decimal('0.001000000') >> "0.001000000"
# Changed
Decimal('0.001000000') >> "0.001"

This is good as a default. But mostly the string representation ends up in a json-response where we don't know how the receiver of the data handles the precision.

An option to trim trailing zeros would be good. It would reduce the amount of data you need to send and you wouldn't loose any data.

By using the decimal builtin .normalize() a decimal can be stripped of it rightmost trailing zeros. reference

By introducing the normalize_output option on DecimalFields it would be possible to opt in on this behaviour and it wouldn't break other peoples code.

Example use:

class MySerializer(Serializer)

  value = fields.DecimalField(max_digits=4, decimal_places=3, normalize_output=True)
# Wanted behaviour
field = fields.DecimalField(max_digits=4, decimal_places=3, normalize_output=True)
output = field.to_representation(Decimal('1.100'))
>> output == '1.1'

Fixes #6151

@Krolken Krolken changed the title Remove trailing zeros on decimal Possibility to remove trailing zeros on DecimalFields representation Mar 19, 2019
@Krolken Krolken requested a review from carltongibson January 20, 2020 07:19
@stefanitsky
Copy link

@carltongibson merge, please 🤓

@PabloLefort
Copy link

Awesome work! What else do we need to merge this? Can I help in something? 🚀

@carltongibson carltongibson removed their request for review September 16, 2020 08:15
@sebabouche
Copy link

sebabouche commented Nov 11, 2020

I will love this feature. Will it be possible to apply it as a setting? Like

REST_FRAMEWORK = {
    ...
    "NORMALIZE_DECIMAL_OUTPUT": True,
    ...
}

@Krolken
Copy link
Contributor Author

Krolken commented Nov 12, 2020

A default setting would be nice

I'm waiting for the review to say all that I done so far is ok and can be merged.
Not sure if it should be added in this PR or it should be its own once this gets merged.

@Krolken Krolken requested a review from tomchristie November 24, 2020 11:12
Copy link

@amirmehdi amirmehdi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is so useful

@elongstreet88
Copy link

elongstreet88 commented Nov 23, 2021

Is this PR still workable or did another workaround get put in place?
Still dealing with trailing zeros in 2021.

@Krolken
Copy link
Contributor Author

Krolken commented Nov 24, 2021

I don't think there is a solution for it yet. There might be a need for a rebase on master but mostly there is a need for a review and sign-off from the maintainers if this solution is favourable and something that should be included.
I would love to merge it. I have 9 decimal points of precision in some projects.

@stale
Copy link

stale bot commented Apr 18, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Apr 18, 2022
@martin056
Copy link

Hi all 👋 I also find this very useful. We've been adding some custom fields to achieve the same behavior in several projects so far. Is there any reason why this is not merged yet? I'd be happy to help if there is anything I could do :)

@stale stale bot removed the stale label Jul 12, 2022
@Krolken
Copy link
Contributor Author

Krolken commented Jul 12, 2022

Hi all 👋 I also find this very useful. We've been adding some custom fields to achieve the same behavior in several projects so far. Is there any reason why this is not merged yet? I'd be happy to help if there is anything I could do :)

Not really sure. I have been waiting for a comment from the maintainers.

@stale
Copy link

stale bot commented Oct 16, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Oct 16, 2022
@stale stale bot closed this Nov 1, 2022
@Rjevski
Copy link

Rjevski commented Nov 16, 2022

Can we reopen this? It's also relevant for me - currently inlined the change into my client's codebase but would be nice to have this upstreamed so we can remove our local overrides.

@tomchristie tomchristie reopened this Nov 16, 2022
@stale stale bot removed the stale label Nov 16, 2022
@tomchristie tomchristie merged commit d5f228d into encode:master Nov 16, 2022
intgr added a commit to typeddjango/djangorestframework-stubs that referenced this pull request Nov 16, 2022
This isn't in any DRF release yet, but fixes typecheck in our CI.

Upstream PR: encode/django-rest-framework#6514
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.

DecimalField: str value without trailing zeros
9 participants