This patch implements a function called format_number_significant_figures() which is like format_number() but rounds to significant figures instead of decimal places. An accompanying patch for formatted_number module will soon follow.

I also wrote simpletests to test the significant figure rounding algorithm, in the attached file. I used these tests with simpletest 2.9 (note this version requires a core patch).

Comments

Bevan’s picture

The related patch for formatted_number module that uses the new function is at #580548: Additional formatters for fixed number of decimal places and significant figures.

Bevan’s picture

The simpletest is a DrupalTestCase, but should really be a DrupalUnitTestCase. There is a bug in simpletest 2.9 that made that more problematic than it was worth though. That bug has now been fixed in Simpletest-dev and should be released in simpletest 2.10, whenever that comes out; #580406: DrupalUnitTestCase requires Database class

Bevan’s picture

StatusFileSize
new2.69 KB

This is the simpletest DRUPAL-6--2 version of the test file that uses DrupalUnitTestCase.

markus_petrux’s picture

Version: 6.x-1.4 » 6.x-1.x-dev
Status: Needs review » Needs work

Could we add a link to a reference on how significant figures algorithm works? This would be fine as a note in the doxygen of the function itself. Then I will update the README and project page myself, but I would appreciate a link.

Bevan’s picture

Any suggestions on where to link to? I got the inspiration for the algo from here on php.net but it was broken and sub-optimal. I rewrote it myself. I contributed the corrected version back to php.net here, which is almost duplicate of what's in this patch.

markus_petrux’s picture

Status: Needs work » Fixed

Committed to CVS (commit). Thanks!

I have not committed the SimpleTest file. I'll add tests to the D7 branch, where the test framework is stable. For D6, it's been in flux, and I don't like the idea to keep supporting this yet.

Bevan’s picture

Fair enough. Note that the signficant figures algo is sensitive so any changes should be tested with these simpletests, at the very least.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

romano321’s picture

Issue summary: View changes

This is a calculator to use for this to check your code accuracy:
sig fig calculator
sig fig rules