Viewed   75 times

This is really weird. I have this piece of code.

$rewardAmt = $amt;
if(is_float($rewardAmt)){
      print_r("is float");die;
} else {
      print_r("is not float"); die;
}

value of $amt is 0.01. But it is going into else condition. So I did a var_dump of $amt. it says string(4) So I decided to typecast $amt

   $rewardAmt = (float)$amt;

But the problem with this is even if the value of $amt is 1, it still gets typecast to float and goes into if condition, which shouldn't happen. Is there any other way to do this ? Thanks

 Answers

2

If you change the first line to

$rewardAmt = $amt+0;

$rewardAmt should be cast to a number.

Sunday, November 6, 2022
4

There's no difference between the PHP versions that would cause this. There are differences between different mysql drivers, however, and that could cause the issue that you are seeing.

Homestead comes with php5-mysqlnd installed, which is the "MySql Native Driver". When using this driver, floating points and integers fetched from the database will be assigned as numeric datatypes in PHP. If you are not using the native driver (php5-mysql), floating points and integers fetched from the database will be assigned as strings in PHP.

The following code demonstrates how this affects your output:

$f = 9.99000;
$s = "9.99000";

echo $f; // shows 9.99
echo $s; // shows 9.99000

You can check to see if the server is using the native driver with the command php -i | grep mysqlnd. This is just searching through your phpinfo() for any mention of the native driver. If this doesn't return anything, then you are not using the native driver, and your numeric data will be returned as strings.

If you do not have the native driver installed, you will need to remove the old driver and install the new driver:

apt-get remove php5-mysql

apt-get install php5-mysqlnd

Assuming this was your issue in the first place, this will fix it. You can also check out this question and answer for more information.

Monday, November 21, 2022
 
5

You should absolutely use isEmpty(). Computing the size() of an arbitrary list could be expensive. Even validating whether it has any elements can be expensive, of course, but there's no optimization for size() which can't also make isEmpty() faster, whereas the reverse is not the case.

For example, suppose you had a linked list structure which didn't cache the size (whereas LinkedList<E> does). Then size() would become an O(N) operation, whereas isEmpty() would still be O(1).

Additionally of course, using isEmpty() states what you're actually interested in more clearly.

Saturday, December 3, 2022
 
shakir
 
1
if ($number % 6 != 0) {
  $number += 6 - ($number % 6);
}

The modulus operator gives the remainder of the division, so $number % 6 is the amount left over when dividing by 6. This will be faster than doing a loop and continually rechecking.

If decreasing is acceptable then this is even faster:

$number -= $number % 6;
Sunday, August 7, 2022
 
1

I presume your dollar amount is of decimal type. So, any value user enters in the field is being cast from string to appropriate type before saving to the database. Validation applies to the values already converted to numeric types, so regex is not really a suitable validation filter in your case.

You have couple of possibilities to solve this, though:

  1. Use validates_numericality_of. That way you leave the conversion completely to Rails, and just check whether the amount is within a given range.
  2. Use validate_each method and code your validation logic yourself (e.g. check whether the value has more than 2 decimal digits).
  3. Validate the attribute before it's been typecasted:

This is especially useful in validation situations where the user might supply a string for an integer field and you want to display the original string back in an error message. Accessing the attribute normally would typecast the string to 0, which isn‘t what you want.

So, in your case, you should be able to use:

validates_format_of :amount_before_type_cast, :with => /^[0-9]+.[0-9]{2}$/, :message => "must contain dollars and cents, seperated by a period"

Note, however, that users might find it tedious to follow your rigid entry rules (I would really prefer being able to type 500 instead 500.00, for example), and that in some locales period is not a decimal separator (if you ever plan to internationalize your app).

Saturday, November 26, 2022
 
jrcs3
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :