Archive

Posts Tagged ‘round’

JavaScript Round To The Nearest 5

April 11th, 2009 Tech 1 comment

I have already talked about a JavaScript function that rounds To the nearest number, but this was only useful if the number needed to be to the nearest 10 (or factor of). The following function is similar, but will round the number to the nearest 5 or 10, depending which is closer.

function round5(x)
{
    return (x % 5) >= 2.5 ? parseInt(x / 5) * 5 + 5 : parseInt(x / 5) * 5;
}

Use the function like this:

alert(round5(12)); // returns 10
alert(round5(14)); // returns 15

Categories: JavaScript Tags: , , , ,

JavaScript Round To Nearest Number

April 11th, 2009 Tech 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;
    }
 }

Round A Number In MS SQL

November 10th, 2008 Tech 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: , , , , ,

Rounding A Number To Nearest The Thousand In PHP

January 6th, 2008 Tech No comments

Talk In Code has previously talked about rounding numbers in PHP, but what if you wanted to round the number to the nearest thousand?

It is possible to do this with the native round() function in PHP by using a negative number as the second parameter. The round() function has two parameters, the first is the number to be rounded and the second is the number of places to round the number to, also known as the precision. The default for round() is to round the number to the nearest whole number, by using positive numbers as the second parameter you can set the number of decimal places to round the number to. Giving a negative number as the second parameter will allow you to round the number to the nearest full number. For example, to round a number to the nearest thousand you can put -3 as the precision, this will change the number 12,345 into 12000.

Here is a full example:
$num = 123456789;
 
echo round($num,0); // whole number - prints 123456789
echo round($num,-1); // ten - prints 123456790
echo round($num,-2); // hundred - prints 123456800
echo round($num,-3); // thousand - prints 123457000
echo round($num,-4); // ten thousand - prints 123460000
echo round($num,-5); // hundered thousand - prints 123500000
echo round($num,-6); // million - prints 123000000
echo round($num,-7); // ten million - prints 120000000
echo round($num,-8); // hundred million - prints 100000000
echo round($num,-9); // billion - prints 0

Note that if you set the precision to be more than the actual number present then you with either get the next highest number or zero. For example, when rounding the number 15, if the precision is -1 then the result is 20 as this is the nearest full ten. However, if -2 is used then the result is 0 as this is the nearest full hundred.

This can cause errors if you are rounding lots of different sized values, so a neat little trick is to use the strlen() function to find out how many characters are in a number and then round to the nearest largest number. You can take the number of characters, subtract 1 to make it one less than the length of the string or you will round some numbers down to zero. You then multiply this value by -1 to invert it.
$num = 151;
$round = (strlen($num)-1)*-1;
echo round($num,$round); // prints 200

Categories: PHP Tags: , , , , ,

Rounding And Displaying Numbers In PHP

December 24th, 2007 Tech No comments

To round a number in PHP you can use one of three functions, these are round(), ceil() and floor(). All of these functions take number as the input and will round the value depending on the function used.

To round to the closest integer use the round() function.

round(4.4); // returns 4

To round down to the nearest whole number use the floor() function.

floor(4.4); // returns 4

To round up to the nearest whole number use the ceil() function.

ceil(4.4); // returns 5

Note that due to the way in which numbers are stored in PHP you can get inconsistent results from rounding a value like 4.5. This is because of the way in which they are stored on the computer. A value of 4.5 might have an actual value of 4.4999999999999999999999 or 4.5000000000000000000001 so when you try to round this value you might get 4 or 5.

To get around this you can add a tiny value to the number before you round it to act as a buffer.

round(4.5 + 0.0000001); // returns 5

The round() function also accepts a second parameter which allows you to set the number of decimal places to round to. In this case round() will round to the nearest significant digit.

round(4.51234,2); // returns 4.51

If you just want to print off the value rather than round it you can use a number of functions to help you do this. The simplest function to use is number_format() which will also add in thousand separators. number_format() takes 4 parameters, but only the first one (the number) is manditory.

number_format(123456.1234); // returns 123,456

The other parameters are

  • decimals - The number of decimal places to round the value to
  • Decimal point - The string to be used as the decimal point.
  • Thousand Separator - The string to be used as a thousands separator.

For example:

echo number_format(123456.123456,2,'POINT','COMMA');
// prints 123COMMA456POINT12

Slightly more complicated is the money_format() function which takes two parameters, the number format and the value. This function is effected by the value set in the LC_MONETARY category of the locale settings. Set this value by using setlocale() before running the function. For more information on the format string see the PHP website.

setlocale(LC_MONETARY, 'en_GB');
$formatString = '%i (after 17.5%% tax)';
echo money_format($formatString, 1234.12) . "\n";
// prints GBP 1,234.12 (after 17.5% tax)

Lastly, you can also use the sprintf() function to format the string and return the output.

echo sprintf("%.2f",123.11);
// prints 123.10