Windows Vista Forums

Show as percent (%)
  1. #1


    EddWood Guest

    Show as percent (%)

    Hi Guys,

    I am new to dotnet and have this custom function to show a £value saving
    based on the ProdPrice and ProdWPrice which works fine, but I am looking for
    the value to be shown as a percentage % rather that a £value for another
    function.


    public static string saving(object ProdPrice, object ProdWPrice)
    {

    if (ProdPrice == null || ProdWPrice == null)
    {
    return "";
    }
    else
    {
    Double ProdPriceNew = Convert.ToDouble(ProdPrice);
    Double ProdWPriceNew = Convert.ToDouble(ProdWPrice);
    Double Saving = Math.Round (Convert.ToDouble(ProdWPriceNew -
    ProdPriceNew),2);

    if (Convert.ToInt32(Saving) <=0)
    {
    return "";
    }
    else
    {
    return "Save " + Saving.ToString("c");
    }
    }

    Thanks

    }




      My System SpecsSystem Spec

  2. #2


    PvdG42 Guest

    Re: Show as percent (%)


    "EddWood" <Edd_nospam_@newsgroup\hotmail.co.uk> wrote in message
    news:9625DD8A-A61A-4DCE-8211-F4EF1FA192FD@newsgroup

    > Hi Guys,
    >
    > I am new to dotnet and have this custom function to show a £value saving
    > based on the ProdPrice and ProdWPrice which works fine, but I am looking
    > for the value to be shown as a percentage % rather that a £value for
    > another function.
    >
    >
    > public static string saving(object ProdPrice, object ProdWPrice)
    > {
    >
    > if (ProdPrice == null || ProdWPrice == null)
    > {
    > return "";
    > }
    > else
    > {
    > Double ProdPriceNew = Convert.ToDouble(ProdPrice);
    > Double ProdWPriceNew = Convert.ToDouble(ProdWPrice);
    > Double Saving = Math.Round (Convert.ToDouble(ProdWPriceNew -
    > ProdPriceNew),2);
    >
    > if (Convert.ToInt32(Saving) <=0)
    > {
    > return "";
    > }
    > else
    > {
    > return "Save " + Saving.ToString("c");
    > }
    > }
    >
    > Thanks
    >
    > }
    You want Saving as a percentage of what? Regardless, it's a matter of doing
    the math. Do whatever division is needed to get the decimalfraction (e.g.
    0.1325). Multiply the result by 100 (0.1325 becomes 13.25). Then add a "%"
    to the output string.

    You can also use the "P" format specifier with ToString() to automate the
    multiplication and addition of the "%".


      My System SpecsSystem Spec

  3. #3


    EddWood Guest

    Re: Show as percent (%)

    Hi Guys,

    To advise, I have sorted it now thanks

    "EddWood" <Edd_nospam_@newsgroup\hotmail.co.uk> wrote in message
    news:9625DD8A-A61A-4DCE-8211-F4EF1FA192FD@newsgroup

    > Hi Guys,
    >
    > I am new to dotnet and have this custom function to show a £value saving
    > based on the ProdPrice and ProdWPrice which works fine, but I am looking
    > for the value to be shown as a percentage % rather that a £value for
    > another function.
    >
    >
    > public static string saving(object ProdPrice, object ProdWPrice)
    > {
    >
    > if (ProdPrice == null || ProdWPrice == null)
    > {
    > return "";
    > }
    > else
    > {
    > Double ProdPriceNew = Convert.ToDouble(ProdPrice);
    > Double ProdWPriceNew = Convert.ToDouble(ProdWPrice);
    > Double Saving = Math.Round (Convert.ToDouble(ProdWPriceNew -
    > ProdPriceNew),2);
    >
    > if (Convert.ToInt32(Saving) <=0)
    > {
    > return "";
    > }
    > else
    > {
    > return "Save " + Saving.ToString("c");
    > }
    > }
    >
    > Thanks
    >
    > }

      My System SpecsSystem Spec

  4. #4


    Michael Ober Guest

    Re: Show as percent (%)

    object.ToString("P") where object is a numeric type will return percentages.

    100.ToString("P") => "100 %"

    Mike.


    "PvdG42" <pvdg42@newsgroup> wrote in message
    news:O57Mp9$fKHA.1648@newsgroup

    >
    > "EddWood" <Edd_nospam_@newsgroup\hotmail.co.uk> wrote in message
    > news:9625DD8A-A61A-4DCE-8211-F4EF1FA192FD@newsgroup

    >> Hi Guys,
    >>
    >> I am new to dotnet and have this custom function to show a £value saving
    >> based on the ProdPrice and ProdWPrice which works fine, but I am looking
    >> for the value to be shown as a percentage % rather that a £value for
    >> another function.
    >>
    >>
    >> public static string saving(object ProdPrice, object ProdWPrice)
    >> {
    >>
    >> if (ProdPrice == null || ProdWPrice == null)
    >> {
    >> return "";
    >> }
    >> else
    >> {
    >> Double ProdPriceNew = Convert.ToDouble(ProdPrice);
    >> Double ProdWPriceNew = Convert.ToDouble(ProdWPrice);
    >> Double Saving = Math.Round (Convert.ToDouble(ProdWPriceNew -
    >> ProdPriceNew),2);
    >>
    >> if (Convert.ToInt32(Saving) <=0)
    >> {
    >> return "";
    >> }
    >> else
    >> {
    >> return "Save " + Saving.ToString("c");
    >> }
    >> }
    >>
    >> Thanks
    >>
    >> }
    >
    > You want Saving as a percentage of what? Regardless, it's a matter of
    > doing the math. Do whatever division is needed to get the decimalfraction
    > (e.g. 0.1325). Multiply the result by 100 (0.1325 becomes 13.25). Then add
    > a "%" to the output string.
    >
    > You can also use the "P" format specifier with ToString() to automate the
    > multiplication and addition of the "%".

      My System SpecsSystem Spec

Show as percent (%) problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
get CPU percent PoShNoob PowerShell 1 15 Mar 2010
45 percent of Google projects in beta dmex Vista News 3 25 Sep 2008
DVD Maker error after 99 percent rwh Vista music pictures video 8 01 Mar 2008
Percent Sign Operator Tom Moreau PowerShell 1 01 Feb 2008
RE: OVER 33 percent are unsatisfied with vista... Daniel- Sydney Vista General 1 14 Oct 2007