Issues PDF export: Spent time/Float-values aren't rounded to 2 digits
|Assignee:||Go MAEDA||% Done:|
Hello,How to simulate problem?
- add Spent time column to issues
- modify Spent time in some issue to 2.45
- export table to PDF
There will be a columns Spent time with 2.45000004768372. I hope there are missing some number format func(), can you please fix this problem? I am making these exports for customers and it looks bad.
Fix: Round floats to 2 digits when rendering an issue list to PDF (#12510).
Patch by Holger Just and Marius BALTEANU.
svn propset svn:eol-style native test/unit/lib/redmine/export/pdf/issues_pdf_test.rb (#12510)
code clean up IssuesPdfHelperTest (#12510)
- remove extra empty lines
- remove trailing white space
- replace tab to two spaces
#5 Updated by Filou Centrinov over 5 years ago
Daniel Felix wrote:
Maybe this relates to the float_max like #12680?
- 0.1 -> IEEE754 Single precision 32-bit
999999999999999999999999999999 -> IEEE754 Single precision 32-bit
UPDATE: No. It does not relate to #12680.
#6 Updated by Filou Centrinov over 5 years ago
- File pdf.patch added
To get pretty float values for spent time and estimated time I created this patch: /lib/redmine/export/ pdf.patch
If the value is empty or zero the pdf cell keeps empty, so you get a better overview on spent and estimated hours.
Could you merge this to redmine?
#9 Updated by Filou Centrinov over 5 years ago
Daniel Felix wrote:
Why not? It seems for me to be again some floating point error, which we've encounter many times in different systems.
The difference is: In #12680 the number is too big, so the floating precision can not be provided. In this issue we are within the floating precision, so the correct presentation should be provided.
The problem is internal caused by displaying a value with a higher precision then the value originaly had. So 0.1 becomes 0.100000001490116, because the type of variable had a precision of 32bit that allows only 8 positions after decimal point. Therefore this problem can easly fixed by:
"%.2f" % value
#10 Updated by Filou Centrinov over 5 years ago
In /lib/redmine/i18n.rb is a function called
l_hours to format hours into a value+label string. If this function would'nt be used in /app/view/issues/show.html.erb you get the same effect of displayed spent time.
l_hours fixes the same "bug", but with an additional label.
#13 Updated by Etienne Massip about 5 years ago
- Status changed from New to Confirmed
- Target version set to Candidate for next minor release
I can confirm the defect although I'm not fond of the patch, is it possible to fix every single float value display at once rather than specific attributes?
Edit: confirmed with MRI 1.9.3
#14 Updated by David Lukas Müller about 5 years ago
For the particular issue I've observed that on Redmine 2.3.1 I had the following time entries:
- 0.30h (I think I've entered "0:20")
5.05with Web User Interface (HTML) --> good
5.050000011920929with PDF-Export --> chould be improved
"spent_hours":5.050000011920929with REST API (JSON) using
http://myhost/redmine/issues/1234.json--> chould be improved
#16 Updated by Toshi MARUYAMA about 5 years ago
You can see by following change.
diff --git a/app/views/issues/_attributes.html.erb b/app/views/issues/_attributes.html.erb --- a/app/views/issues/_attributes.html.erb +++ b/app/views/issues/_attributes.html.erb @@ -63,7 +63,7 @@ <% end %> <% if @issue.safe_attribute? 'estimated_hours' %> -<p><%= f.text_field :estimated_hours, :size => 3, :disabled => !@issue.leaf?, :required => @issue.required_attribute?('estimated_hours') %> <%= l(:field_hours) %></p> +<p><%= f.text_field :estimated_hours, :size => 6, :disabled => !@issue.leaf?, :required => @issue.required_attribute?('estimated_hours') %> <%= l(:field_hours) %></p> <% end %> <% if @issue.safe_attribute?('done_ratio') && @issue.leaf? && Issue.use_field_for_done_ratio? %>
#17 Updated by Holger Just almost 4 years ago
The attached patch (which was extracted from Planio) fixes this issue for all float values in table cells. This patch thus addresses the concerns of Etienne above.
The patch works agains current trunk but should apply similarly to earlier versions. The used method is the one used in the
QueriesHelper#column_value method and thus should produce results similar to it.
#21 Updated by S. Ruttloff 10 months ago
Problem still exists in Redmine 3.3.2, but could fix it here:
if value.is_a?(Date) format_date(value) elsif value.is_a?(Time) format_time(value) else value end
and replace by
if value.is_a?(Date) format_date(value) elsif value.is_a?(Time) format_time(value) elsif value.is_a?(Float) sprintf "%.2f", value else value end
#22 Updated by Marius BALTEANU 10 months ago
- File 0001-Round-floats-to-2-digits-when-rendering-an-issue-lis_r17037.patch added
- File before.png added
- File after.png added
- Assignee deleted (
Tested on current trunk (r17037) and the issue still occurs.
I've updated the patch from Holger Just to apply cleanly. I think that we can fix this issue in the next release.
#25 Updated by Marius BALTEANU 10 months ago
- File 0001-Round-floats-to-2-digits-when-rendering-an-issue-lis_r17055.patch added
Mischa The Evil wrote:
We might want some test coverage added if there isn't any yet, as Jean-Baptiste already suggested.
Attached an updated patch that contains the fix and a test strictly for this scenario. On top of this patch, we can add more tests in the future to improve the coverage.
Marius, thank you for adding a test. But I encountered the following error. Could you look into it?
$ ruby test/unit/lib/redmine/export/pdf/issues_pdf_test.rb Run options: --seed 32491 # Running: F Failure: IssuesPdfHelperTest#test_fetch_row_values_should_round_float_values [test/unit/lib/redmine/export/pdf/issues_pdf_test.rb:32]: --- expected +++ actual @@ -1 +1 @@ -["2", "Add ingredients categories", "4.34"] +["2", "Add ingredients categories", "0.00"] bin/rails test test/unit/lib/redmine/export/pdf/issues_pdf_test.rb:23 Finished in 0.658523s, 1.5185 runs/s, 1.5185 assertions/s. 1 runs, 1 assertions, 1 failures, 0 errors, 0 skips
#31 Updated by Marius BALTEANU 10 months ago
It should be fine now.