change negative sign

i have a total figure shows -200 and i want to make it display as 200..




Answer this question

change negative sign

  • AvalonNewbie

    >> to change any number to negative, just multiply the number with -1, i.e., x = x * -1

    Even simpler, just tack on a negative sign:

    x = - x;



  • mikeret

    now i have another case..

    i have a positive value which i want to conver to negative



  • Amnu cherian

    to change any number to negative, just multiply the number with -1, i.e., x = x * -1.
  • Genetic1234

    Hi there,

    Is this total you have in a numeric variable (e.g. int, double, short etc) If it is, you can use the System.Math.Abs() function as this will return the absolute value of whatever number is passed to it. For example:

    Double
    test = -200.97;
    Double
    nonNegTest = System.Math.Abs(test);

    MessageBox.Show("Original: " + test.ToString());
    MessageBox.Show("Alteration: " + nonNegTest.ToString());

    The message boxes above are for illustration only so that it can be seen what the effects of System.Math.Abs() are.


    However, if your total is stored in a string variable or the like, you can convert the value to something numeric (using one of the Convert.ToXXX() functions) then use
    System.Math.Abs() and then place the non-negative result inside your original variable (using something like the ToString() function). For example:

    string test = "-200";

    int conversionOfTest = Convert.ToInt32(test);

    int nonNegVersionOfTest = System.Math.Abs(conversionOfTest);

    MessageBox.Show("Original: " + test);
    test = nonNegVersionOfTest.ToString();

    MessageBox.Show("Modified: " + test);

    You could streamline the above a lot more but for an example I think it will do. Once again, the message boxes are just for illustration of what has happened.

    Hope that helps a bit.


  • change negative sign