how to make negative numbers into positive

前端 未结 8 1870
天命终不由人
天命终不由人 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:37

    this is the only way i can think of doing it.

    //positive to minus
    int a = 5; // starting with 5 to become -5
    int b = int a * 2; // b = 10
    int c = a - b; // c = - 5;
    std::cout << c << endl;
    //outputs - 5
    
    
     //minus to positive
    int a = -5; starting with -5 to become 5
    int b = a * 2; 
    // b = -10
    int c = a + b 
    // c = 5
    std::cout << c << endl;
    //outputs 5
    

    Function examples

    int b = 0;
    int c = 0;
    
    
    int positiveToNegative (int a) {
        int b = a * 2;
        int c = a - b;
        return c;
    }
    
    int negativeToPositive (int a) { 
        int b = a * 2;
        int c = a + b;
        return c; 
    }
    

提交回复
热议问题