Changing the sign of a number in PHP?

前端 未结 8 1343
星月不相逢
星月不相逢 2021-01-31 13:26

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         


        
8条回答
  •  深忆病人
    2021-01-31 13:52

    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;
      

提交回复
热议问题