Skip to content

BUG: Ensure value to string conversion account for precision. Fixes #4044

Sam Horvath requested to merge github/fork/jcfr/fix-units into master

Created by: jcfr

In addition to improve the Units logic API to facilitate the registration of new units node, this topic fixes issue #4044.

In a nutshell, calling vtkMRMLUnitNode::GetDisplayStringFromValue() was returning incorrect string. The following snippet illustrates the issue and confirm that this topic effectively address the problem.

import math
l = slicer.modules.units.logic()
s = l.GetUnitsScene()
n = s.GetNodesByName("Metre per second").GetItemAsObject(0)
n.SetPrecision(3)
for v in range(-5, 5):
  print("10^%d -> %s" % (v, n.GetDisplayStringFromValue(math.pow(10, v))))

Before the change:

10^-5 ->  1e-05 m/s
10^-4 ->  0.0001 m/s
10^-3 ->  0.001 m/s
10^-2 ->  0.01 m/s
10^-1 ->  0.1 m/s
10^0 ->  1 m/s
10^1 ->  10 m/s
10^2 ->  100 m/s
10^3 ->  1e+03 m/s
10^4 ->  1e+04 m/s

After the change:

10^-5 ->  0.000 m/s
10^-4 ->  0.000 m/s
10^-3 ->  0.001 m/s
10^-2 ->  0.010 m/s
10^-1 ->  0.100 m/s
10^0 ->  1.000 m/s
10^1 ->  10.000 m/s
10^2 ->  100.000 m/s
10^3 ->  1000.000 m/s
10^4 ->  10000.000 m/s

Merge request reports