Archive

Posts Tagged ‘number’

JavaScript Round To Nearest Number

April 11th, 2009 No comments

The JavaScript Math.round() function will round a decimal to the nearest whole number, but I found that I needed was to round a number to the nearest 10. So after a bit of thinking I realised that I could divide the value by 10, round it using the round() function and then multiply the result by 10.

So taking it a step further I decided to create a function that would round a number to the nearest 10, 100, 1000 or whatever value is entered. If this value is less than 0 then do the reverse by multiplying and dividing the number by the given value of accuracy. Here is the function.

function roundNearest(num, acc){
    if ( acc < 0 ) {
        num *= acc;
        num = Math.round(num);
        num /= acc;
        return num;
    } else {
        num /= acc;
        num = Math.round(num);
        num *= acc;
        return num;
    }
}

Here are some tests of the function.

alert( roundNearest(12345, 1000) ); // 1200
alert( roundNearest(12345.1234, -100) ); //12345.12

This function can be simplified a little by putting all of the calculations onto one line.

function roundNearest(num, acc){
    if ( acc < 0 ) {
        return Math.round(num*acc)/acc;
    } else {
        return Math.round(num/acc)*acc;
    }
 }

PHP Function To Detect A Prime Number

April 9th, 2009 1 comment

A prime number is a number which has exactly two distinct number divisors: 1 and itself. So if you take the number 11, it can only be divided to get a whole number if it is divided by 1 or 11. If any other number is used then a fraction is always found.

The following function uses a method called trial division to detect if a number is prime or not.

function is_prime($number)
{
    // 1 is not prime
    if ( $number == 1 ) {
        return false;
    }
    // 2 is the only even prime number
    if ( $number == 2 ) {
        return true;
    }
    // square root algorithm speeds up testing of bigger prime numbers
    $x = sqrt($number);
    $x = floor($x);
    for ( $i = 2 ; $i <= $x ; ++$i ) {
        if ( $number % $i == 0 ) {
            break;
        }
    }
    
    if( $x == $i-1 ) {
        return true;
    } else {
        return false;
    }
}

The function first detects if the number is 1 (not prime) or if it is two (prime). These are two exceptions to the rules that follow and must be caught before proceeding. The function divides the number by all numbers less than or equal to the square root of that number. If any of the divisions come out as an integer, then the original number is not a prime. Otherwise, it is a prime.

Here is an example bit of script that finds all of the prime numbers between 0 and 1,000,000.

$start = 0;
$end =   1000000;
for($i = $start; $i <= $end; $i++)
{
    if(is_prime($i))
    {
        echo '<strong>'.$i.'</strong>, ';
    }
}

Obviously this takes a little while to run!

Also, this function is only useful if you want to check integers, if your number is higher than the maximum value of an integer PHP will use a float to store the number, which causes false positives. To find the maximum value of an integer on your system use the following code.

echo PHP_INT_MAX;

Categories: PHP Tags: , , , , ,

Something To Be Aware Of JavaScript isNaN

March 17th, 2009 1 comment

The isNaN() function (NaN stands for Not a Number) can be useful if you are looking at form inputs or similar and is used to detect if a value is not a number. For example, the following code shows the output of isNaN() on two variables.

var number42 = "42";
var wibble = "wibble";
alert(isNaN(number42)); // Prints out false
alert(isNaN(wibble)); // Prints out true

This first test is true because the number 42 is, in fact, a number. The second test is false because wibble isn’t a number. This is simple enough, but what if we started looking at some different values?

var empty = "";
var nothing = null;
alert(isNaN(empty)); // Prints out false
alert(isNaN(nothing)); // prints out false

What is happening here is that isNaN() tries to converts any value it is given into an number, which means that "" and null get converted to 0 and the function returns false because they are now numbers. You can try this out yourself by doing one of the following:

var empty = "";
alert(+empty); // Prints out 0
alert(Number(empty)); // Prints out 0

To solve this problem you can use the parseInt() function. This takes a string as an input and tries to convert it into a number, if this is not possible then it returns NaN. If parseInt() is given an empty string or a null value it will also return NaN, rather than converting them to 0. This gives us a mechanism by which we can test values using a combination of parseInt() and isNaN().

var empty = "";
alert(isNaN(parseInt(empty))); // Prints out true

Finally, if we give isNaN() and parseInt() an undefined value the results are as follows.

var nothing;
alert(parseInt(nothing) + ", " + isNaN(nothing)); // Prints out NaN, true

The isNaN() function returns true because it can’t convert the value into any number.

var nothing;
alert(+nothing);
alert(Number(nothing));

This code prints out NaN for both of the alert statements.

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: , , , , ,

Find The Number Of Days For A Given Month With PHP

October 10th, 2008 No comments

There are two ways to find out the number of days for a given month. The first is to use the date() function in conjunction with the mktime() function to create a date and format this value as the number of days in a given month.

$monthDays = date("t",mktime(0,0,0,12,1,2008));

The second way is to use the function cal_days_in_month(). This function takes three parameters.

  • Canelday: There are a number of different forms of calendar to chose from. The one most English speaking people are interested in is the CAL_GREGORIAN calendar. Take a look at a full list of calendar constands.
  • Month: An integer representing the month (1 – 12).
  • Year: An integer representing the year.

So to find out the number of days in the month of December of 2008 you could use the following.

$monthDays = cal_days_in_month(CAL_GREGORIAN, 12, 2008);

Categories: PHP Tags: , , , , , , ,