Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Possibility to remove trailing zeros on DecimalFields representation#6514

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

@Krolken
Copy link
Contributor

@KrolkenKrolken commentedMar 19, 2019
edited
Loading

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.

# CurrentDecimal('0.001000000')>>"0.001000000"# ChangedDecimal('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 thenormalize_output option on DecimalFields it would be possible to opt in on this behaviour and it wouldn't break other peoples code.

Example use:

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

Fixes#6151

Anthropologist, sorinstelian, st4lk, bialesdaniel, romasku, stefanitsky, PabloLefort, saschwarz, qulaz, bennettrogers, and 6 more reacted with thumbs up emoji
@KrolkenKrolken changed the titleRemove trailing zeros on decimalPossibility to remove trailing zeros on DecimalFields representationMar 19, 2019
@stefanitsky
Copy link

@carltongibson merge, please 🤓

@PabloLefort
Copy link

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

@carltongibsoncarltongibson removed their request for reviewSeptember 16, 2020 08:15
@sebabouche
Copy link

sebabouche commentedNov 11, 2020
edited
Loading

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

REST_FRAMEWORK = {    ...    "NORMALIZE_DECIMAL_OUTPUT": True,    ...}
amirmehdi and anyidea reacted with thumbs up emoji

@Krolken
Copy link
ContributorAuthor

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.

Copy link

@amirmehdiamirmehdi 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

Krolken reacted with thumbs up emoji
@elongstreet88
Copy link

elongstreet88 commentedNov 23, 2021
edited
Loading

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

martin056 reacted with thumbs up emoji

@Krolken
Copy link
ContributorAuthor

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.

ldesgrange, elongstreet88, and mdefeche reacted with thumbs up emoji

@stale
Copy link

stalebot commentedApr 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.

@stalestalebot added the stale labelApr 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 :)

@stalestalebot removed the stale labelJul 12, 2022
@Krolken
Copy link
ContributorAuthor

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.

martin056 reacted with thumbs up emoji

@stale
Copy link

stalebot commentedOct 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.

@stalestalebot added the stale labelOct 16, 2022
@stalestalebot closed thisNov 1, 2022
@Rjevski
Copy link

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.

@stalestalebot removed the stale labelNov 16, 2022
@lovelydinosaurlovelydinosaur merged commitd5f228d intoencode:masterNov 16, 2022
intgr added a commit to typeddjango/djangorestframework-stubs that referenced this pull requestNov 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 freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@lovelydinosaurlovelydinosaurAwaiting requested review from lovelydinosaur

1 more reviewer

@amirmehdiamirmehdiamirmehdi approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

DecimalField: str value without trailing zeros

9 participants

@Krolken@stefanitsky@PabloLefort@sebabouche@elongstreet88@martin056@Rjevski@lovelydinosaur@amirmehdi

[8]ページ先頭

©2009-2025 Movatter.jp