How do i make a negative number positive in php?

A trivial

$num = $num <= 0 ? $num : -$num ;

or, the better solution, IMHO:

$num = -1 * abs($num)

As @VegardLarsen has posted,

the explicit multiplication can be avoided for shortness but I prefer readability over shortness

I suggest to avoid if/else (or equivalent ternary operator) especially if you have to manipulate a number of items (in a loop or using a lambda function), as it will affect performance.

"If the float is a negative, make it a positive."

In order to change the sign of a number you can simply do:

$num = 0 - $num;

or, multiply it by -1, of course :)

❮ PHP Math Reference

Example

Return the absolute value of different numbers:

echo(abs(6.7) . "
");
echo(abs(-6.7) . "
");
echo(abs(-3) . "
");
echo(abs(3));
?>

Try it Yourself »


Definition and Usage

The abs() function returns the absolute (positive) value of a number.


Syntax

Parameter Values

ParameterDescription
number Required. Specifies a number. If the number is of type float, the return type is also float, otherwise it is integer

Technical Details

Return Value:The absolute value of the number
Return Type:Float / Integer
PHP Version:4+

❮ PHP Math Reference


View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The abs() function is an inbuilt function in PHP which is used to return the absolute (positive) value of a number. The abs() function in PHP is identical to what we call modulus in mathematics. The modulus or absolute value of a negative number is positive.

    Syntax:

    number abs( value )

    Parameters: The abs() function accepts single parameter value which holds the number whose absolute value you want to find.

    Return Value: It returns the absolute value of the number passed to it as argument.

    Examples:

    Input : abs(6)
    Output : 6
    
    Input : abs(-6)
    Output : 6
    
    Input : abs(6.4)
    Output : 6.4
    
    Input : abs(-6.4)
    Output : 6.4
    

    Below programs illustrate the abs() function in PHP:

    Program 1: When a positive number is passed as a parameter:

    Output:

    6

    Program 2: When a negative number is passed as a parameter:

    Output:

    6

    Program 3: When a positive number with decimal places is passed as a parameter:

    Output:

    6.4

    Program 4: When a negative number with decimal places is passed as a parameter:

    php

    echo (abs(-6.4);

    ?>      

    Output:

    6.4

    Reference: http://php.net/manual/en/function.abs.php


    This is a short tutorial on how to convert negative numbers into positive numbers using PHP. In this guide, we will show you how to use PHP’s abs function.

    Take a look at the following snippet.

    //For the sake of this tutorial, let us say that our variable contains -6
    $ourNegativeNumber = -6;
    
    //To convert this minus number into a positive number, we simply use PHP's abs function.
    $positiveNumber = abs($ourNegativeNumber);
    
    //Print it out onto the screen.
    echo $positiveNumber;
    
    //The result should be 6.

    In the code above, we converted the number -6 to 6 using PHP’s abs function. The abs function will return the absolute number of a given variable.

    In case you didn’t already know, an absolute value is the non-negative value of a number.

    For example, the absolute “version” of -10 is 10.

    Note that the abs function also works with float values and decimal numbers.

    //A negative decimal number
    $ourNegativeDecimal = -6.24;
    
    //The abs function will also convert negative decimal numbers into positive decimals.
    $positiveDecimal = abs($ourNegativeDecimal);
    
    //Print out our float value onto the screen.
    echo $positiveDecimal; //becomes 6.24

    The return type from the abs function depends on the variable that you provide it with. For example, if you provide it with a float value, then the return type will also be a float. Otherwise, it will return an integer value.

    How do you turn a negative into a positive?

    All you have to do just multiply a negative value with -1 and it will return the positive number instead of the negative.

    How do you make a value negative in PHP?

    PHP abs() Function "
    "); echo(abs(-6.7) . "
    ");

    How do you convert a negative number to a positive power query?

    You could also highlight the column in the query editor and right click, select transform, and choose absolute value. That would give the positve number outcome you're looking for. @Len - You might have to wrap it in an IF statement if you want to preserve already positive numbers.

    How do I turn a negative number positive in HTML?

    Use the abs() method.
    //given negative integer. let n = -20; ​.
    //print original n. console. log("Given negative integer n = " + n); ​.
    //convert n to positive integer. n = Math. abs(n) ​.