Purpose

You want to convert a floating-point number to decimal.

Some SAP tables contain values in floating-point format. When you read such tables with, for example, /guixt/dbselect, a number in floating-point notation is returned, such as:

  • 1.5301000000000000E+03
  • Or with a comma as the decimal separator: 1,5301800000000000E+03

For further calculations and comparisons in GuiXT, you need the decimal representation of the number.

Solution
Use the JavaScript toFixed() funcion.

The following utility function returns the decimal format of a given number in floating point format. You may sepecify a fixed number of decimal places as a second argument.

JavaScript
// convert a number from floating point format to decimal format
// option: specify fixed number of decimals
function Float2Dec(x,fixdec)
{

   var deccomma =x.indexOf(',') > -1;
   if (deccomma)
   {
     x = x.replace(",", ".");
   }; 

   if (isNaN(parseFloat(x))) 
   {
    return 0;
   }

   
  var result; 
  if (fixdec)
  {
    result = parseFloat(x).toFixed(fixdec);
  }
  else 
  {
    result = parseFloat(parseFloat(x).toFixed(8)).toString() ;
  };

  
   
   if (deccomma)
   {
     result= result.replace(".", ",");
   }; 

   return result; 
};


Examples
GuiXT
Set V[f1] "1.5301800000000000E+03"
CallJS f2 = Float2Dec &V[f1]
 
 // Test message
 Message "&V[f2]" 


GuiXT
Set V[f1] "1.5301800000000000E+03"
CallJS f2 = Float2Dec &V[f1] 1
...
					


GuiXT
Set V[f1] "1.5301800000000000E+03"
CallJS f2 = Float2Dec &V[f1] 5
...					


GuiXT
Set V[f1] "1.5301805600E+03"
CallJS f2 = Float2Dec &V[f1]
...
						


Components
InputAssistant + Controls