search for in the

 Last updated: Sat, 07 Jan 2012

abs

(PHP 4, PHP 5)

abs절대값

설명

number abs ( mixed \$number )

number의 절대값을 반환합니다.

number

처리할 수치 값

반환값

number의 절대값. number 인수의 자료형이 float일 경우, 반환값도 float이고, integer일 경우에도 마찬가지입니다. (일반적으로 float 값의 범위가 integer 값의 범위보다 큽니다)

예제

Example #1 abs() 예제

``` <?php\$abs = abs(-4.2); // \$abs = 4.2; (double/float)\$abs2 = abs(5);   // \$abs2 = 5; (integer)\$abs3 = abs(-5);  // \$abs3 = 5; (integer)?> ```

User Contributed Notes abs - [2 notes]
svein dot tjonndal at gmail dot com
2 years ago
``` If you don't have/want GMP and are working with large numbers/currencies: <?php function mb_abs(\$number) {   return str_replace('-','',\$number); } ?> No need to worry about encoding, as your numbers should all be basic (ANSI) strings. ```
-3
Ister
4 years ago
``` [*EDIT* by danbrown AT php DOT net: Merged user's corrected code with previous post content.] jeremys indicated one thing - there is no sgn function wich actually seems a bit strange for me. Of course it is as simple as possible, but it is usefull and it is a standard math function needed occasionally. Well, I have solved this function in a bit different matter: <?php function sgn(\$liczba) {     if(\$liczba>0)         \$liczba=1;     else if(\$liczba<0)         \$liczba=-1;     else if(!is_numeric(\$liczba))         \$liczba=null;     else         \$liczba=0;     return \$liczba; } ?> The difference is that it returns null when the argument isn't a number at all. ```