Numerical parameter passed to script evaluates as NaN... sometimes

Until I can figure out another issue, I’m trying a work around.

I’m trying to return something like this for an order tag that isn’t updating with product quantity.

Let’s say product quantity is 3 and tag price is .1 - I’ll pass off to a script the order quantity and the tag price:

{CALL:recyclingFee.format('{QUANTITY}','{ORDER TAG PRICE}')}

function format(quantity,price)
  SambaLog('Quantity: '+quantity);
  SambaLog('Price: '+price);
  SambaLog('Quantity (TN): '+q);
  SambaLog('Price (TN): '+p);
  var q = Number(quantity);
  var p = Number(price);  
  var total = eval(quantity*price);
  return quantity+' Recycling Fee|'+total;

And it should return * 3 Recycling Fee 0.30

Instead it returns * 3 Recycling Fee NaN

The script recognises quantity 3 in the return statement, yet when it comes to any sort of math it’s all-of-a-sudden not a number.

Logging shows this:

18/10/2020 14:45:37.131  [Debug]  Quantity: {QUANTITY}
18/10/2020 14:45:37.189  [Debug]  Price: 0.10
18/10/2020 14:45:37.245  [Debug]  Quantity (TN): NaN
18/10/2020 14:45:37.303  [Debug]  Price (TN): 0.1

How is it for printing quantity in the return statement works, but dumping the var value results in the report tag?

Arnt you using q and p before var defined?
Also not familier with eval() but is you use q and p couldnt you just do q*p since you already forced number?

Sorry, that was my fuckup on copy and paste mid ctrl-z or something; at this point, I dunno.

eval, nasty though it may be, can be used with strings containing numbers. I was just trying stuff to rule out possibilities.

Manually testing, everything works fine:




I’m still perplexed how the string returns fine in one location, but not in another.

Maybe wrap the input expressions in TN()

1 Like

Normally I would, but [=] expressions don’t work inside ternary expressions.

Number() input received a non valid expression, look at:

Number(true);          // returns 1
Number(false);         // returns 0
Number("10");          // returns 10
Number("  10");        // returns 10
Number("10  ");        // returns 10
Number(" 10  ");       // returns 10
Number("10.33");       // returns 10.33
Number("10,33");       // returns NaN
Number("10 33");       // returns NaN
Number("John");        // returns NaN

What I’ve done is converting the value to string using .toString() and replacing any character that causas NaN value in Number function, I see the value is with right Syntax but if you pass a number to number() or returns NaN or the number? I’ve not tried that.

1 Like