IBM Support What's New?

Change the display precision of numbers using OPL script

Technote (FAQ)


How do I change the display precision of numbers using OPL script?


The following OPL script code changes the display precision of numbers. Please note that this only changes the way that numbers gets printed when they are serialized, like printing an array of floats or in the output of the printSolution method but not while printing the individual array element itself. Also note that if the precision is set to N, then the display would show N+1 decimal places; the default value being 4 (and thus 5 decimal places). Ofcourse, this value can also be changed through an ops file under the 'Language -> General -> Display precision' section. If you would like to round the actual values of the elements to a certain precision level, then you would need to implement your own roundTo function (similar to what the sample does) to achieve the desired results.

range R = 1..3;
float a[r in R] = r / 7;
execute beforePrecisionSetting{
  writeln("Before setting precision");
  for(var x in R)
execute precisionSetting{
  writeln("Setting displayPrecision=2");
execute afterPrecisionSetting{
  writeln("After setting precision");
  for(var x in R)
execute roundingFunction{
  function roundTo(x, digits){
   return x;
  for(var x in R)
  writeln("After rounding to 2 decimal places");
  for(x in R)

Cross reference information
Segment Product Component Platform Version Edition
Commerce IBM ILOG CPLEX Optimization Studio Area:ILOG Script for OPL/OPL Script AIX, Linux, Windows 12.4, 12.3,, 12.2 All Editions

Document information

More support for: IBM ILOG CPLEX Optimization Studio
Area:ILOG Script for OPL/OPL Script

Software version: 6.0, 6.0.1, 6.1, 6.1.1, 6.2, 6.3, 12.2, 12.3, 12.4

Operating system(s): AIX, Linux, Windows

Software edition: All Editions

Reference #: 1615825

Modified date: 2012-11-14