We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12294
    • 30 Posts
    Hi,

    This might have absolutely nothing to do with modx, but I’m getting to the point of tearing my hair out and I’m hoping someone here can help.

    Currently I pass two tv’s as parameters on a snippet. When I call them immedietly upon reaching the snippet they print out fine. However once I try and convert the string to an integer, in order to do some basic maths on it, for some reason it converts the string to 0. I’ve tried several different methods, and I’m wondering if there is something that I am missing, as this conversion has worked fine on normal php files in the past.

     echo "This is the type of initial netPrice:";
     echo gettype($netPrice);
     echo "<br/>";
     echo $netPrice. " = NET PRICE<br/>";
     echo intval($netPrice, 10); 
     $test = (int)$netPrice;
     echo gettype($test);
     echo "<br/>";
     echo $test. " = Net Price as an int<br/>";
    


    Which then prints out:

    This is the type of initial netPrice:string
    10.99 = NET PRICE
    0
    integer
    0 = Net Price as an int
    0 Test2
    0 Test

    I hope someone can spot a really simple mistake!

    Thanks

    Hollie
      • 16278
      • 928 Posts
      Your code works fine for me pasted into a simple snippet using MODX 1.0.3, Win 7, PHP 5.3.1 and a text TV. What type of TV is it? Perhaps the string is not as simple as it looks - try checking its length with strlen(), for instance. If it’s not 5 for 10.99, perhaps there are some hidden extras in there. You could check it with is_numeric(), also.
      smiley KP
        • 12294
        • 30 Posts
        Managed to get this working, turns out that all I needed to do was install phx, which I had forgotten about!

        Thanks for the help!

        :)
          • 18373 ☆ A M B ☆
          • 3,141 Posts
          When you’re going to calcuate with the numbers, it’s best to specify the price in cents (1099) instead of a float (10.99).

          You’ll get inaccuracies if you use floats.. tried to find the article I read about, but couldn’t find it instantly. Will try to find it later tongue
            Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

            Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.