how to make negative numbers into positive

前端 未结 8 1765
天命终不由人
天命终不由人 2021-02-01 14:09

I am having the negative floating point number as:

a = -0.340515;

to convert this into positive number I used the abs() method as:



        
相关标签:
8条回答
  • 2021-02-01 14:53

    abs() is for integers only. For floating point, use fabs() (or one of the fabs() line with the correct precision for whatever a actually is)

    0 讨论(0)
  • 2021-02-01 14:55

    You have to use:

    abs() for int
    fabs() for double
    fabsf() for float

    Above function will also work but you can also try something like this.

        if(a<0)
        {
             a=-a;
        }
    
    0 讨论(0)
提交回复
热议问题