在 PHP 中更改数字的符号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1438111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:34:29  来源:igfitidea点击:

Changing the sign of a number in PHP?

phpmathnumberssign

提问by dotty

I have a few floats:

我有几个浮点数:

-4.50
+6.25
-8.00
-1.75

How can I change all these to negative floats so they become:

我怎样才能将所有这些更改为负浮点数,使它们变为:

-4.50
-6.25
-8.00
-1.75

Also I need a way to do the reverse

我也需要一种方法来做相反的事情

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

如果浮点数为负数,则将其设为正数。

回答by drAlberT

A trivial

一个琐碎的

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

or, the better solution, IMHO:

或者,更好的解决方案,恕我直言:

$num = -1 * abs($num)

As @VegardLarsen has posted,

正如@VegardLarsen 所发布的,

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/else(或等效的三元运算符),特别是如果您必须操作多个项目(在循环中或使用lambda 函数),因为它会影响性能。

"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 :)

或者,乘以-1,当然:)

回答by Vegard Larsen

$float = -abs($float);

回答by Gumbo

How about something trivial like:

一些琐碎的事情怎么样:

  • inverting:

    $num = -$num;
    
  • converting only positive into negative:

    if ($num > 0) $num = -$num;
    
  • converting only negative into positive:

    if ($num < 0) $num = -$num;
    
  • 反转:

    $num = -$num;
    
  • 仅将正数转换为负数:

    if ($num > 0) $num = -$num;
    
  • 仅将负值转换为正值:

    if ($num < 0) $num = -$num;
    

回答by SilentGhost

re the edit: "Also i need a way to do the reverse If the float is a negative, make it a positive"

重新编辑:“我还需要一种方法来做相反的事情如果浮动是负数,则将其设为正数”

$number = -$number;

changes the number to its opposite.

将数字更改为相反的数字。

回答by Dan Tao

I think Gumbo's answer is just fine. Some people prefer this fancy expression that does the same thing:

我认为 Gumbo 的回答很好。有些人更喜欢这种做同样事情的花哨表达:

$int = (($int > 0) ? -$int : $int);

EDIT: Apparently you are looking for a function that will make negatives positive as well. I think these answers are the simplest:

编辑:显然你正在寻找一个函数,它也会使底片变成正片。我认为这些答案是最简单的:

/* I am not proposing you actually use functions called
   "makeNegative" and "makePositive"; I am just presenting
   the most direct solution in the form of two clearly named
   functions. */
function makeNegative($num) { return -abs($num); }
function makePositive($num) { return abs($num); }

回答by Wallace Maxters

function positive_number($number)
{
    if ($number < 0) {
        $number *= -1;
    }

   return $number;
}

回答by Frederik Krautwald

function invertSign($value)
{
    return -$value;
}

回答by Marco Limas

using alberT and Dan Tao solution:

使用 alberT 和 Dan Tao 解决方案:

negative to positive and viceversa

从负到正,反之亦然

$num = $num <= 0 ? abs($num) : -$num ;