Merge lp:~mcbits/pantheon-calculator/fix-1415049 into lp:~elementary-apps/pantheon-calculator/trunk

Proposed by James McManus
Status: Needs review
Proposed branch: lp:~mcbits/pantheon-calculator/fix-1415049
Merge into: lp:~elementary-apps/pantheon-calculator/trunk
Diff against target: 18 lines (+7/-1)
1 file modified
src/Core/Evaluation.vala (+7/-1)
To merge this branch: bzr merge lp:~mcbits/pantheon-calculator/fix-1415049
Reviewer Review Type Date Requested Status
MarkoD (community) Needs Fixing
elementary Apps team Pending
Review via email: mp+312514@code.launchpad.net

Commit message

Fix calculator bug #1415049 where some calculations produce negative 0

Description of the change

Fix calculator bug #1415049 by rounding results to the given precision and ensuring any 0.0 results are positive before formatting for display.

To post a comment you must log in.
Revision history for this message
MarkoD (markodolar) wrote :

Looks like with this fix calculator now properly outputs number of decimal places :)

But this also introduces small bug - if d_places is 0 and equation result is "0", calculator will crash in cut() trying to slice radix character off an empty string.

review: Needs Fixing

Unmerged revisions

296. By James McManus <email address hidden>

Fix calculator bug #1415049 where some calculations produce negative 0

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/Core/Evaluation.vala'
2--- src/Core/Evaluation.vala 2015-04-03 12:04:47 +0000
3+++ src/Core/Evaluation.vala 2016-12-06 00:03:47 +0000
4@@ -276,7 +276,13 @@
5 }
6
7 private string cut (double d, int d_places) {
8- var s = ("%.5f".printf (d));
9+ // First round to the given decimal places and ensure
10+ // anything that rounds to -0.0 becomes positive 0.0.
11+ double scale = pow10(d_places);
12+ double rounded_value = round(d * scale) / scale + 0.0;
13+
14+ // Convert to string and strip trailing 0s.
15+ var s = ("%.*f".printf (d_places, rounded_value));
16 while (s.last_index_of ("0") == s.length - 1)
17 s = s.slice (0, s.length - 1);
18 if (s.last_index_of (Posix.nl_langinfo (Posix.NLItem.RADIXCHAR)) == s.length - 1)

Subscribers

People subscribed via source and target branches

to all changes: