locked
How to convert a number to percentage RRS feed

  • Question

  • this is my number 9.858 after conversion to percentage how could i get this output 9.86%. how to convert 9.858 to 9.86% ?

    give me sample code.

    i tried this but did not get output figure as 9.86%.

    my code

                decimal amt = 9.858M;
                amt = Math.Round(amt, 2);
                MessageBox.Show("Percentage " +string.Format("{0:p2}", amt) + "  " + string.Format("{0:p3}", amt));
    

    Friday, December 14, 2018 12:23 PM

Answers

  • Because target value is multiplied by 100. Math.Round is not necessary.

    Your code should be:

    decimal amt = 0.09858M;
    MessageBox.Show("Percentage " +string.Format("{0:p2}", amt) + "  " + string.Format("{0:p3}", amt));

    • Marked as answer by Sudip_inn Wednesday, January 9, 2019 6:24 PM
    Friday, December 14, 2018 1:32 PM
  • Here are several language extensions

    using System;
    
    namespace ExtensionsLibrary
    {
        public static class NumericExtensions
        {
            /// <summary>
            /// Calculates a value's percentage of another value.
            /// </summary>
            /// <param name="sender">Value to compare.</param>
            /// <param name="pTotalValue">Total value to compare original to.</param>
            /// <returns>Integer percentage value.</returns>
            public static int PercentageOf(this double sender, double pTotalValue) => Convert.ToInt32(sender * 100 / pTotalValue);
            /// <summary>
            /// Calculates a value's percentage of another value.
            /// </summary>
            /// <param name="sender">Value to compare.</param>
            /// <param name="pTotalValue">Integer percentage value.</param>
            /// <returns></returns>
            public static int PercentageOf(this decimal sender, decimal pTotalValue) => (int) Convert.ToDecimal(sender * 100 / pTotalValue);
            /// <summary>
            /// Calculates a value's percentage of another value.
            /// </summary>
            /// <param name="sender">Value to compare.</param>
            /// <param name="pTotalValue">Total value to compare original to.</param>
            /// <returns>Integer percentage value.</returns>
            public static int PercentageOf(this long sender, long pTotalValue) => Convert.ToInt32(sender * 100 / pTotalValue);
            /// <summary>
            /// Determine if sender is event
            /// </summary>
            /// <param name="sender"></param>
            /// <returns></returns>
            public static bool IsEven(this int sender) => sender % 2 == 0;
        }
    }
    

    Example

    decimal item = 12M;
    var result = item.PercentageOf(100);
    Console.WriteLine($"Percent of 100 is {result}");


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Sudip_inn Wednesday, January 9, 2019 6:24 PM
    Friday, December 14, 2018 1:30 PM
  • Hi Sudip,

    From what you posted, it sounds like the number has already been converted to a percentage, you just need to display it … is that correct? In that case, you'd want to divide it by 100 in the string.Format():

    amt = 9.858M;
    // amt = Math.Round(amt, 2); Don't need the round
    Console.WriteLine("Percentage " + string.Format("{0:p2}", amt/100) + "  " + string.Format("{0:p3}", amt/100));


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com


    Friday, December 14, 2018 2:56 PM

All replies

  • Here are several language extensions

    using System;
    
    namespace ExtensionsLibrary
    {
        public static class NumericExtensions
        {
            /// <summary>
            /// Calculates a value's percentage of another value.
            /// </summary>
            /// <param name="sender">Value to compare.</param>
            /// <param name="pTotalValue">Total value to compare original to.</param>
            /// <returns>Integer percentage value.</returns>
            public static int PercentageOf(this double sender, double pTotalValue) => Convert.ToInt32(sender * 100 / pTotalValue);
            /// <summary>
            /// Calculates a value's percentage of another value.
            /// </summary>
            /// <param name="sender">Value to compare.</param>
            /// <param name="pTotalValue">Integer percentage value.</param>
            /// <returns></returns>
            public static int PercentageOf(this decimal sender, decimal pTotalValue) => (int) Convert.ToDecimal(sender * 100 / pTotalValue);
            /// <summary>
            /// Calculates a value's percentage of another value.
            /// </summary>
            /// <param name="sender">Value to compare.</param>
            /// <param name="pTotalValue">Total value to compare original to.</param>
            /// <returns>Integer percentage value.</returns>
            public static int PercentageOf(this long sender, long pTotalValue) => Convert.ToInt32(sender * 100 / pTotalValue);
            /// <summary>
            /// Determine if sender is event
            /// </summary>
            /// <param name="sender"></param>
            /// <returns></returns>
            public static bool IsEven(this int sender) => sender % 2 == 0;
        }
    }
    

    Example

    decimal item = 12M;
    var result = item.PercentageOf(100);
    Console.WriteLine($"Percent of 100 is {result}");


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Sudip_inn Wednesday, January 9, 2019 6:24 PM
    Friday, December 14, 2018 1:30 PM
  • Because target value is multiplied by 100. Math.Round is not necessary.

    Your code should be:

    decimal amt = 0.09858M;
    MessageBox.Show("Percentage " +string.Format("{0:p2}", amt) + "  " + string.Format("{0:p3}", amt));

    • Marked as answer by Sudip_inn Wednesday, January 9, 2019 6:24 PM
    Friday, December 14, 2018 1:32 PM
  • Hi Sudip,

    From what you posted, it sounds like the number has already been converted to a percentage, you just need to display it … is that correct? In that case, you'd want to divide it by 100 in the string.Format():

    amt = 9.858M;
    // amt = Math.Round(amt, 2); Don't need the round
    Console.WriteLine("Percentage " + string.Format("{0:p2}", amt/100) + "  " + string.Format("{0:p3}", amt/100));


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com


    Friday, December 14, 2018 2:56 PM
  • Hi Sudip_inn,

    Is there any update? if the issue still exists, please feel free let us know.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 24, 2018 8:32 AM