Archive

Posts Tagged ‘ceil’

Some Useful Maths Functions In JavaScript

January 14th, 2008 2 comments

All of the maths functions in JavaScript are kept in a handy object called Math, which contains a number of different functions.

To get the absolute value of a number use the abs() function.
Math.abs(3.14159265) // returns 3.14159265

Rounding a number is done by either the round() function to round to the nearest integer, the ceil() function to round up to the nearest integer and the floor() function to round down to the nearest integer.
Math.ceil(3.14159265) // returns 4
Math.floor(3.14159265) // returns 3
Math.round(3.14159265) // returns 3

To find the exponent of a number use the exp() function.
Math.exp(3.14159265) // returns 23.140692549708973

The log() method returns the natural logarithm (base E) of a number.
Math.log(3.14159265) // returns 1.1447298847067335

Raising a number by a value is done with the pow() function, the two parameters are the number and the power.
Math.pow(3.14159265) // returns 4.114463202429791

Finding the square root of a number is done by using the sqrt() function.
Math.sqrt(3.14159265) // returns 1.7724538498928541

A pseudo random number between 0 and 1 can be found by using the random() function.
Math.random() // returns a number between 0 and 1

A group of trigonometric functions also exist.
Math.tan(1.5707963267948966) // returns 16331778728383844
Math.cos(1.5707963267948966) // returns 6.123031769111886e-17
Math.sin(1.5707963267948966) // returns 1
Math.atan(1.5707963267948966) // returns 1.0038848218538872
Math.atan2(1.5707963267948966,1.23567) // arc tangent of a/b - returns 0.9042475963309642
Math.acos(0.5) // returns 1.0471975511965976
Math.asin(0.5) // returns 0.5235987755982989

All of the trigonometric functions work with radians and not degrees so you will need to convert degrees to radians if that is what you are working in. To convert radians to degrees multiply the value by PI divided by 180. To convert degrees to radians multiply the value by 180 divided by PI.
// degrees to radians
var rad = (90*(Math.PI/180)); // returns 1.5707963267948966
// radians to degrees
var deg = (1.5707963267948966*(180/Math.PI)); // returns 90

To compare numbers you can use the min() and max() functions. The min() function will return the smallest number from a set of values and max() will returns the largest number from a set of values.
Math.max(1,2,3,4,5,6,7,8,9) // returns 9
Math.min(1,2,3,4,5,6,7,8,9) // returns 1

Additionally, there are a few constant values available in the Math object, these are as follows.

  • Math.E – Euler’s constant (also known as e) = 2.718281828459045
  • Math.LN2 – Natural log of the value 2 = 0.6931471805599453
  • Math.LN10 – Natural log of the value 10 = 2.302585092994046
  • Math.LOG2E – The base 2 log of e = 2.302585092994046
  • Math.LOG10E – The base 10 log of e = 0.4342944819032518
  • Math.PI – Pi = 3.141592653589793
  • Math.SQRT1_2 – The square root of one half = 0.7071067811865476
  • Math.SQRT2 – The square root of 2 = 1.4142135623730951

There are also some constants kept in the Number object. These are mainly used to validate integers and are as follows.

  • Number.MAX_VALUE – The largest value of an integer that can be used in JavaScript. This value is 1.7976931348623157e+308
  • Number.MIN_VALUE – The smallest value of an integer that can be used in JavaScript. This value is 5e-324.
  • Number.NaN – Used to indicate that a value is not a number.
  • Number.NEGATIVE_INFINITY – The value returned if a negative overflow occurs. Any numeric value divided by this is 0.
  • Number.POSITIVE_INFINITY – The value returned if a positive value overflow occurs. Any numeric value divided by this is 0.

All of these constants can be used as validation. For example, trying to run a function with an integer that is above the maximum limit can cause errors, so a simple checksum can be put in place.
if(value1*value2 <= Number.MAX_VALUE){
  function1(value1)
}else{
  function2(value1)
}

Rounding And Displaying Numbers In PHP

December 24th, 2007 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