使所有数字为负数

发布于 2024-08-04 22:51:10 字数 201 浏览 2 评论 0原文

我有一些浮动:

-4.50
+6.25
-8.00
-1.75

我怎样才能将所有这些更改为负浮动,以便它们变成:

-4.50
-6.25
-8.00
-1.75

此外,我还需要一种方法来进行相反的操作,

如果浮动是负数,请将其设为正数。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

吻泪 2024-08-11 22:51:10

一个简单的

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

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

$num = -1 * abs($num)

正如@VegardLarsen 所发布的那样,

为了简短,可以避免显式乘法,但我更喜欢可读性而不是简短

我建议避免 if/else (或等效的三元运算符),特别是如果您必须操作多个项目(在循环中或使用 lambda 函数 /em>),因为它会影响性能。

“如果浮动值为负数,则将其设为正值。”

为了更改数字的符号,您可以简单地执行以下操作:

$num = 0 - $num;

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

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

寂寞笑我太脆弱 2024-08-11 22:51:10
$float = -abs($float);
$float = -abs($float);
寂寞陪衬 2024-08-11 22:51:10

一些琐碎的事情怎么样:

  • 反转:

    <前><代码>$num = -$num;

  • 仅将正数转换为负数:

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

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

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;
    
凡尘雨 2024-08-11 22:51:10

重新编辑:“我还需要一种方法来进行相反的操作,如果浮动是负值,则将其设为正值”

$number = -$number;

将数字更改为其相反值。

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.

独孤求败 2024-08-11 22:51:10

我认为Gumbo的回答很好。有些人更喜欢这种奇特的表达方式,它可以做同样的事情:

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

编辑:显然您正在寻找一个可以使负数也变为正数的函数。我认为这些答案是最简单的:

/* 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); }

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

$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); }
后eg是否自 2024-08-11 22:51:10
function positive_number($number)
{
    if ($number < 0) {
        $number *= -1;
    }

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

   return $number;
}
白云悠悠 2024-08-11 22:51:10
function invertSign($value)
{
    return -$value;
}
function invertSign($value)
{
    return -$value;
}
情愿 2024-08-11 22:51:10

使用alberT和丹涛解决方案:

从负到正,反之亦然

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

using alberT and Dan Tao solution:

negative to positive and viceversa

$num = $num <= 0 ? abs($num) : -$num ;
苦笑流年记忆 2024-08-11 22:51:10

下面是一个简单的可重用代码,用于反转int、float、double 或decimal 的符号。
如果值为正,该方法将返回负值,反之亦然。

function inverseSign($value)
{
    return $value * (-1);
}

Here's a simple reusable code to inverse the sign of an int, float, double or decimal.
If the value is positive, the method will return a negative value and vice versa.

function inverseSign($value)
{
    return $value * (-1);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文