NUMBER OPERATIONS - MX

Declaration

Numeric variables:

int: Integer (numbers without decimal points)

long: Twice the size of an int, 64 bits, from -2^63 to 2^63

float: Number with a decimal point

double: Twice the size of a float

One can either directly call _str* variables, or declare new "containers":

String zone = _str1;

double rugosite = Str.ToDouble(_str2);

Conversions

=== From double to int ====

int i = (int) Str.ToDouble(_str1);
// or
double Data = Str.ToDouble(_str1) * 60.0 * 60.0;
int i = (int)Data;

=== String to Double ====

// Use "double" (with a lowercase d)
double d= Str.ToDouble(_str1);

=== String to long ====

long l= Str.ToLong(_str1);

=== String to int ====

int i=Str.ToIntOrZero(_str1);

=== Int & double to String ====

String toto=String.valueOf(i);

Troubleshooting

Variables

When storing numbers with decimals in a variable, always use "double" and not "Double".
In other words, no capital "d" in "double".

double d=1;
// It is a good idea to prepend a "d" before the variable name, that indicates that it is a double.
double dPuissance=Str.ToDouble(_str1);

Warning about scientific notation

'''If your number is so small that it is stored in scientific notation (eg 3.14E-4), if you return it as such you will have trouble parsing the results:'''

double d = 0.000314;
return d;
// Result: 3.14E-4

If you want an actual number to work with, you need to use String.format:

double d = 0.000314;
return String.format("%.2f", d);
// Result: 0.00

If you want additional digits after the decimal place, change "%.2f" to "%.8f" for example:

double d = 0.000314;
return String.format("%.8f", d);
// Result: 0.00031400

NB: This formatting works by '''rounding''', not '''truncating''':

double a = 0.0006;
return String.format("%.3f", a);
// Result: 0.001
double a = 0.0004;
return String.format("%.3f", a);
// Result: 0.000
double a = 0.0005;
return String.format("%.3f", a);
// Result: 0.001

Mathematical operations

Multiplication, division, addition and substraction

return (Str.ToDouble(_str1)*Str.ToDouble(_str2)/100.0/12.0) + Str.ToDouble(_str3)-6.0;
//==============================
// New MX Common function 
//==============================
return MxMath_Add(_doJava, _str1, _str2);      // _str1 + _str2
return MxMath_Substrat(_doJava, _str1, _str2); // _str1 - _str2
return MxMath_Multiply(_doJava, _str1, _str2); // _str1 * _str2
return MxMath_Divide(_doJava, _str1, _str2);   // _str1 / _str2

Absolute value of a number

return Math.abs(Str.ToDouble(_str1));
//==============================
// New MX Common function 
//==============================
return MxMath_Abs(_doJava, _str1); // |_str1|

Power of a number (works also with negative powers)

return Math.pow(Str.ToDouble(_str1),3);
//==============================
// New MX Common function 
//==============================
return MxMath_Power(_doJava, _str1,_str2); // _str1^(_str2) 
return MxMath_Power(_doJava, _str1, "3");  // _str1^3  You can do that, but you have to set _str2 equal to whatever constant for now

Square root

return Math.sqrt(Str.ToDouble(_str1));
//==============================
// New MX Common function 
//==============================
return MxMath_sqrt(_doJava, _str1);

Rounding to the nearest integer

return Math.round(Str.ToDouble(_str1));
//==============================
// New MX Common function 
//==============================
return MxMath_Round(_doJava, _str1);

Pi and angles manipulation

The number Pi

return Math.PI*10.5*10.5*76*Str.ToDouble(_str1)/100;

Cosinus, Sinus, Tangente

return Math.cos(Str.ToDouble(_str1));
return Math.sin(Str.ToDouble(_str1));
return Math.tan(Str.ToDouble(_str1));
//==============================
// New MX Common function 
//==============================
return MxMath_Cos(_doJava, _str1); // cos(_str1)
return MxMath_Sin(_doJava, _str1); // sin(_str1)
return MxMath_Tan(_doJava, _str1); // tan(_str1)

Subtracting the value on a previous line to the value on the current line

return Str.ToDouble(_str1)-Str.ToDouble(_doJava.get("_str1",-1));

Counting occurrences over time

(Example with the number of melees in a rugby match)

_str1 = type of action and _str2 = my own java program "nbr of melees"

if (_doJava.get("_str2",-1) ##null)
return "0";

int melee_prcdt = (Str.ToIntOrZero(_doJava.get("_str2",-1)) + 1);
if (!_str1.equals("Mélée"))
return _doJava.get("_str2",-1);
return Integer.toString(melee_prcdt);

Computing the sign of an expression

Returns 1.0 if the expression is positive, 0.0 if it is zero, and -1.0 if it is negative. Useful for avoinding 3 if statements

double d=1.5;
double signe=Math.signum(d); // Returns 1.0
d=0.0;
signe=Math.signum(d);        // Returns 0.0
d=-3.14;
signe=Math.signum(d);        // Returns -1.0

Differentiate even and odd number (nombre pair et impair)

if (Str.ToDouble(_str1) % 2 ##0) 
return "Pair";
if (Str.ToDouble(_str1) % 2 != 0) 
return "Impair";
return "";
//==============================
// New MX Common function 
//==============================
return MxMath_is_Odd_Even(_doJava, _str1);

Calculate the average for 6 numbers (for java multiple, without using GroupByColumn)

double sum = Str.ToDouble(_str1)+Str.ToDouble(_str2)+Str.ToDouble(_str3)
+Str.ToDouble(_str4)+Str.ToDouble(_str5)+Str.ToDouble(_str6);

 int number=0;
 if(!_str1.equals(""))
 number = number +1;

 if(!_str2.equals(""))
 number = number +1;

  if(!_str3.equals(""))
 number = number +1;

 if(!_str4.equals(""))
 number = number +1;

  if(!_str5.equals(""))
 number = number +1;

  if(!_str6.equals(""))
 number = number +1;

return sum/number;

Averaging values contained in variables separated by

Ex: _str1 contains :
"#199.67#199.67#199.67#199.65#199.65#199.65#199.65#199.65#199.65#199.65#199.65#199.65#199.61"

java.util.StringTokenizer st = new java.util.StringTokenizer(_str1,"#");
double d=0;
int nCount=0;
while (st.hasMoreTokens())
{
    String str=st.nextToken();
    d+=Str.ToDouble(str);
    nCount++;
}

if (nCount==0)
  return "";
else
  return d / nCount;

Was this article helpful?

Powered by Zendesk