awidgery
awidgery

Reputation: 1916

Django floatformat formatting 113.999 to 114.0

I have a value that I'm passing to a template which is 113.999.

When I do {{ value|floatformat:"-1" }} it outputs 114.0.

View code: some_var = 113.999

Template code: {{ some_var|floatformat }}

My understanding of floatformat was that it should round, and then not display the decimal part if it was all zeroes.

Is floatformat wrong, or am I?

Upvotes: 2

Views: 702

Answers (1)

okm
okm

Reputation: 23871

Interesting, by exploring the code, I've found that the quantizing happens after the checking of integer. Thus 113.999 is quantized after it is checked against integer and failed.

IMO, well I'm not an expert of number notation :), 114.0 here represents a rounded-up value and it's not an originally 114.

If you really don't want this behavior, a quick and awkward way is
{{ value|floatformat|floatformat }}...

Furthermore, floatformat uses -1 by default, thus {{ value|floatformat }} is enough in your code.

Upvotes: 1

Related Questions