django-rest-framework icon indicating copy to clipboard operation
django-rest-framework copied to clipboard

Possibility to remove trailing zeros on DecimalFields representation

Open Krolken opened this issue 5 years ago • 9 comments

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 avatar Mar 19 '19 16:03 Krolken

@carltongibson merge, please 🤓

stefanitsky avatar Sep 09 '20 14:09 stefanitsky

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

PabloLefort avatar Sep 13 '20 22:09 PabloLefort

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

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

sebabouche avatar Nov 11 '20 23:11 sebabouche

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 avatar Nov 12 '20 08:11 Krolken

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

elongstreet88 avatar Nov 23 '21 22:11 elongstreet88

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.

Krolken avatar Nov 24 '21 08:11 Krolken

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[bot] avatar Apr 18 '22 07:04 stale[bot]

Hi all :wave: 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 :)

martin056 avatar Jul 12 '22 06:07 martin056

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.

Krolken avatar Jul 12 '22 07:07 Krolken

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[bot] avatar Oct 16 '22 06:10 stale[bot]

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.

Rjevski avatar Nov 16 '22 14:11 Rjevski