Results 1 to 4 of 4

Thread: Problem with two Values

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2020
    Posts
    21

    Problem with two Values

    HTML Code:
     private void profitbefore()
            {
                decimal final = 0;
                decimal fullfinal = 0;
                if(decimal.TryParse(txtprofitbefore.Text,out final) && decimal.TryParse(txtfinal.Text,out fullfinal))
                {
                    
                    decimal finalfull = final - fullfinal;
                    txtbeforeexp.Text = finalfull.ToString("#,##0.00");
                }
            }
    i have two values in windows from the final value have the negative value when i perform the operation its sum the negative value mathematic that's correct but is there any solution we can covert the nagtive valie into postive i search a lot on stackoverflow but i can't get my right answer

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,836

    Re: Problem with two Values


  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,712

    Re: Problem with two Values

    If you always want a positive number consider the following language extension.

    Code:
    public static class Extensions
    {
        public static T ToPositive<T>(this T source) where T : INumber<T> 
            => T.IsNegative(source) ? -source : source;
    }
    Usage
    Code:
    int value = -5;
    value = value.ToPositive();

  4. #4
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    813

    Re: Problem with two Values

    Don't know if this is any good for you.

    Code:
                int neg = -5;
                if(neg < 0)
                {
                    neg = (-neg);
                }
                MessageBox.Show
                    (neg.ToString());

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width