Archive

Posts Tagged ‘integer’

Round A Number In MS SQL

November 10th, 2008 No comments

To round a number in MS SQL use the ROUND() function. This function takes two parameters, the first is the number to be rounded and the second is the number of decimal places to round the number to. Here is an example of rounding the numbers in a column to the nearest whole integer.

SELECT ROUND(table.column1,0) rentValue FROM table

Categories: MS SQL Tags: , , , , ,

String Equals Zero In PHP

September 8th, 2008 1 comment

Due to the weakly-typed nature of PHP you can do some odd things, some of which are good, and some of which will enable you to shoot yourself in the foot. Take the following little snippet.

echo '1' + 5;

In some languages this might cause the program to fall over, but PHP will try to evaluate any string into an integer. In this case it converts the string to an integer 1 and adds this to 5 to make 6.

As an aside, if you did this in JavaScript then you would find the opposite result. Because the concatenation character is the same as the addition character JavaScript will always try to truncate the value if any of the present values are a string. So the result in JavaScript would be "15".

If we change the string to a string of "one" and then did the same then the result is 5.

echo 'one' + 5;

This is because if PHP can’t translate the string to an integer it will assume that it is 0.

We can take this to another level by using comparison. Looking at the PHP type comparison tables you can see that there is a lot of different ways that two values can be compared.

Take a look at the following snippet. What would expect the outcome of this to be?

$a = 'a string';
$b = 0;
 
if($a == true && $b == false && $a == $b){
 exit;
}

Well the answer is that the program would exit because all of these comparisons equal true.

'a string' == true equates to true because PHP will evaluate any non empty string to true if compared with a boolean.

0 == false equates to true because the integer 0 is evaluated as false when compared with a boolean.

'a string' == 0 also evaluates to true because any string is converted into an integer when compared with an integer. If PHP can’t properly convert the string then it is evaluated as 0. So 0 is equal to 0, which equates as true.

To get around this issue you can use the === operator in place of the == operator. This operator (also called triple-equals operator) will only evaluate to true not only if the two values have the same value, but also if they are the same type. So if we changed the example to use the triple-equals operator then all of the terms would evaluate to false. This is because a string cannot be a boolean, an integer cannot be a boolean and a string does not equal an integer.

The difference between the two equals operators is important to remember. Each has it’s own use, but if you are in any doubt of the type of your values then use the triple-equals operator, especially when passing the test would spell disaster for your program.

The triple-equals operator is essential when using functions like strpos(). This is because it will return false when the string is not found. As in the following example where the $position variable equals false.

$position = strpos('abcd','z');

But what happens if the string is found at position 0? If you used the double-equals operator then you will find that your position will equate to false.

PHP Function To Turn Integer To Roman Numerals

April 22nd, 2008 No comments

Use the following function to change any integer into a string containing the integer value as a Roman Numeral.

function integerToRoman($integer){
 // Convert the integer into an integer (just to make sure)
 $integer = intval($integer);
 $result = '';
 
 // Create a lookup array that contains all of the Roman numerals.
 $lookup = array('M' => 1000,
 'CM' => 900,
 'D' => 500,
 'CD' => 400,
 'C' => 100,
 'XC' => 90,
 'L' => 50,
 'XL' => 40,
 'X' => 10,
 'IX' => 9,
 'V' => 5,
 'IV' => 4,
 'I' => 1);
 
 foreach($lookup as $roman => $value){
  // Determine the number of matches
  $matches = intval($integer/$value);
 
  // Add the same number of characters to the string
  $result .= str_repeat($roman,$matches);
 
  // Set the integer to be the remainder of the integer and the value
  $integer = $integer % $value;
 }
 
 // The Roman numeral should be built, return it
 return $result;
}

To run the code just give the function an integer. Here are some examples.

echo integerToRoman(1).'<br />';
echo integerToRoman(42).'<br />';
echo integerToRoman(123).'<br />';
echo integerToRoman(4576).'<br />';
echo integerToRoman(1979).'<br />';

This will print out the following.

I
XLII
CXXIII
MMMMDLXXVI
MCMLXXIX

You can also give the function a string.

echo integerToRoman('765').'<br />';

Will print out DCCLXV.

Categories: PHP Tags: , , ,

Does A String In PHP Contain A Number?

December 24th, 2007 No comments

The is_numeric() function in PHP can be used to see if a number contained in a string is numeric. The function returns true is the variable can be parsed into a string, otherwise it returns false. Here are some examples:

is_numeric('five') // returns false
is_numeric(123); // returns true
is_numeric('123'); // returns true
is_numeric(-123); // returns true
is_numeric('-123'); // returns true
is_numeric('123.4'); // returns true
is_numeric('1,234'); // returns false

Notice that if your number has a thousand separator in it the function will return false. In this case you need to use the str_replace() function to strip out the commas before passing the value into is_numeric(0).

is_numeric(str_replace(',','','1,234')); // returns true

Remember that by default all GET and POST variables will be passed to PHP as strings so this function is useful when checking form input as it can verify that a value is a number before you go on to check the value of that number.

PHP also has a variety of other functions to allow you to check that a variable is a number of a specific type. These are is_bool(), is_float() (also known as is_double() or is_real(), is_int() (also known as is_integer()), and is_long().