0

Anyone knows why the calculation field returns an error when converting a string into an integer?

var $prix = Max of Prix de vente; //Value is 2000.00
var $titre = All of Nom du produit;
var $nbr_heure = $titre[0].substring(0, 3).trim(); //Value is "25"
parseInt($nbr_heure)

parseInt($prix) returns 2000

I have tried parseFloat() and Number(). It seems like as soon as it tried to convert a string, it doesn't process.

My static workaround

var $prix = Max of Prix de vente;
var $titre = All of Nom du produit;
var $nbr_heure = $titre[0].substring(0, 3).trim();
var $taux_horaire = 0;
//Conversion stupide manuelle
if($nbr_heure == "25")
  $taux_horaire = $prix / 25;
else if($nbr_heure == "50")
  $taux_horaire = $prix / 50;
else if($nbr_heure == "100")
  $taux_horaire = $prix / 100;
else
  $taux_horaire = 89;
$taux_horaire;

More info, if you do this:

$prix + parseInt($nbr_heure); //Same error

and

$prix + $nbr_heure; //Gives 200025  (String concatenation)

Thank you for any feedback!

Vincent Poirier
  • 4,298
  • 5
  • 21
  • 25
  • What's the error message? – Andreas Haugstrup Pedersen Aug 26 '14 at 21:46
  • `Le résultat n'est pas un nombre valide` which means `The result is not a valid number`. I discovered a bug in the calculation field as well where if the first result type was a string upon saving, it will expect that same type forever afterward when modifying unless you delete the field and make a new one. – Vincent Poirier Aug 27 '14 at 11:33

1 Answers1

0

You are getting the error because parseInt() is returning NaN instead of an actual number. Or because your substring call is failing.

If your $titre field doesn't contain a value your substring call will fail. So make sure to check in your calculation that it is working with a value and not undefined.

In a similar vein parseInt will return NaN if you call it with an empty string.

  • Check out these two screenshots: http://dev.magikweb.ca/hotlink/parseint-podio.png – Vincent Poirier Aug 28 '14 at 11:56
  • Removing the unit (heures) doesn't change anything either. (In case it was included in the parseInt call) According to your answer, could it be that the returned type is not a native javascript one but a PodioCustomClass ? – Vincent Poirier Aug 28 '14 at 11:59
  • In your second field try replacing `parseInt($nbr_heure);` with `parseInt("25");` -- that should work. I'm using parseInt() myself in calculation fields, but you have to make sure that the input is what you think it is. If your `$titre` variable ends up being empty you will get errors. So you have to check in your calculation whether or not the input to parseInt() is valid or not. – Andreas Haugstrup Pedersen Aug 29 '14 at 21:41
  • You're right, it does work with a manual string. But in my screenshot, if it shows a result of "25" after the string manipulations, what else can it be and why wouldn't `parseInt()` work? That screenshot is the same field, I made it show the result "25" then tried using `parseInt()` right away. Sorry for dragging this on, I'm having a hard time understanding this. – Vincent Poirier Aug 30 '14 at 01:06