Skip to main content

Change the display precision of numbers using OPL script


Technote (FAQ)


Question

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

Answer

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");
  writeln("a="+a);
  for(var x in R)
   writeln("a["+x+"]="+a[x]);
}  
execute precisionSetting{
  settings.displayPrecision=2;
  writeln("Setting displayPrecision=2");
}
execute afterPrecisionSetting{
  writeln("After setting precision");
  writeln("a="+a);
  for(var x in R)
   writeln("a["+x+"]="+a[x]);
}
execute roundingFunction{
  function roundTo(x, digits){
   x=x*Opl.pow(10,digits);
   x=Opl.round(x);
   x=x/Opl.pow(10,digits);
   return x;
  }
  for(var x in R)
   a[x]=roundTo(a[x],2);
  writeln("After rounding to 2 decimal places");
  writeln("a="+a);
  for(x in R)
   writeln("a["+x+"]="+a[x]);
}


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

Rate this page:

(0 users)Average rating

Copyright and trademark information

IBM, the IBM logo and ibm.com are trademarks of International Business Machines Corp., registered in many jurisdictions worldwide. Other product and service names might be trademarks of IBM or other companies. A current list of IBM trademarks is available on the Web at "Copyright and trademark information" at www.ibm.com/legal/copytrade.shtml.

Rate this page:


(0 users)Average rating

Add comments

Document information

IBM ILOG OPL-CPLEX Development Bundles

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

Translate my page

Content navigation