none
New but curious

    Question

  • Hi all,

    Ive been toying with Blend for the last few days aiming to write for WP7.

    My question is regarding number formats.

    IF i create a simple Click calculation, is there a setting to assign:

    the values as a percentage, and also the number of trailing digits.

    I am manually changing to % via math in code, but is there a better way? And I cannot seem to find how to set trailing digits.

    Hopefully I am being clear, exscuse me if not. :)

    My code is as 

    private void btnGetRest_Click(object sender, RoutedEventArgs e)
            {
               tbYDAYPE.Text = (Convert.ToDouble(tbYD.Text) * Convert.ToDouble(tbAY.Text) * Convert.ToDouble(tbPE.Text)* 100 + "%").ToString();
            }
    	}
    Thursday, January 05, 2012 11:39 AM

Answers

  • Thank you.

    Works perfectly.

    And I accept those tips graciously. I am at the beginning of a long learning curve as you can see.

    Smile

    Thursday, January 05, 2012 1:14 PM

All replies

  • Math.Round(value, decimalPoints) will allow you to round a number to the specified number of decimal places


    http://msdn.microsoft.com/en-us/library/75ks3aby(v=VS.95).aspx

    Thursday, January 05, 2012 12:08 PM
  • Thank you,

    I used the code below which returns a string error if I include the + "%" and is great if i dont, is there a workaround fo this?

    :

    tbYDAYPE.Text = Math.Round((Convert.ToDouble(tbYD.Text) * Convert.ToDouble(tbAY.Text) * Convert.ToDouble(tbPE.Text)* 100),2 + "%").ToString();
    
    
    
    
    Thursday, January 05, 2012 12:46 PM
  • calculate the number before you append the percent symbol, silly:

    tbYDAYPE.Text = Math.Round((Convert.ToDouble(tbYD.Text) * Convert.ToDouble(tbAY.Text) * Convert.ToDouble(tbPE.Text)* 100),2 ).ToString() + "%";

    your code might be easier to read and debug if you broke it into a few lines, as well.  single line code is neat, but when it gets long, it helps to separate concerns.  [no IDE, please forgive any trivial errors]

    var total = Convert.ToDouble(tbYD.Text) * Convert.ToDouble(tbAY.Text) * Convert.ToDouble(tbPE.Text)* 100;
    var rounded = Math.Round(total, 2);
    tbYDAYPE.Text = rounded + "%";
    Thursday, January 05, 2012 12:53 PM
  • Thank you.

    Works perfectly.

    And I accept those tips graciously. I am at the beginning of a long learning curve as you can see.

    Smile

    Thursday, January 05, 2012 1:14 PM