4

I have SQL table with a column with a data type of Float. There is an external app that writes percentage values to this column and I read them and display them in a web app.

I understand how to convert decimal's to percentages; usually I would multiply by 100, but this is a little different.

The values in the DB look like this:

 0.95  <-- DB  5%  <-- UI
 0.85  <-- DB  15% <-- UI
 0.5   <-- DB  50% <-- UI

I have tried

number * percentage / 100

and

((percentage /100) * value).ToString();

and

 string percentage = string.Format("Percentage is {0:0.0%}", value);

and

 percentage/100m*value

and

 myDecimal.ToString("0.00");

and finally, what I thought was close:

Math.Round(myDecimal, 2).ToString("{0.00}");

and of course various string formatting things like

 MyDecimal.ToString("P"), .ToString("P1"), etc)

Articles on Formatting, Rounding and Converting Percentages in C#:

Convert percentage to nearest fraction, Working percentage in c#, http://www.dotnetperls.com/percentage,

But I can not for the life of me find a math calculation or built in c# conversion to give me the output I need.

What it seems like I need to do is get the 0.96 digits to be subtracted by some number and then in turn multiply that by 100, but the 0.5 being 50% always throws my math off....

This seems like it would be a common conversion; what I am missing here?

UPDATE

Here is a the solution I ended up with, which works great! (Thanks Reed)

            var li = new List<Object>();
        var retVal = new List<string>();

        li.Add(.5);       // 50%
        li.Add(.95);      // 95%
        li.Add(.85);      // 85%
        li.Add(0.87813);  // 12.18700%

        foreach (var o in li)
        {
            var value = Convert.ToDouble(o);
            string percentage = string.Format("Percentage is {0:0.0%}", 1.0 - value);

            retVal.Add(percentage);
        }

LINKS

For the sake of completeness, here is a pretty consise list of resources on Decimals, Percentages, Doubles, and conversions in C#:

A+

Working percentage in c#
Format decimal for percentage values?
Convert percentage to nearest fraction
http://www.dotnetperls.com/percentage
.NET: Decimal to rounded string
C# Is there a built-in function to convert a formatted String back to a Number?

A-

Format decimal as a percent with specific decimal places
Convert decimal to percent or shift decimal places. How
How to convert percentage string to double?

B+

two ways of displaying a decimal
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

Community
  • 1
  • 1
Shawn J. Molloy
  • 2,457
  • 5
  • 41
  • 59
  • 3
    `1 - 0.95 = 0.05` then `0.05 * 100 = 5` then format to string and put `%` on end – T I Feb 05 '13 at 19:49

3 Answers3

5

I believe you'd want:

string percentage = string.Format("Percentage is {0:0.0%}", 1.0 - value);

If you just want the number:

string percentage = (1.0 - value).ToString("P");

Or, the number without decimal precision:

string percentage = (1.0 - value).ToString("P0");
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Thanks for your quick response Reed; I think half of the math articles I read were answered by you! I will try this solution out now. – Shawn J. Molloy Feb 05 '13 at 20:07
  • You are a god among devs, Mr Copsey. This works wonderfully! Thanks again for your answer - I've updated my question with my test code which proves this does work flawlessly. – Shawn J. Molloy Feb 05 '13 at 20:14
  • A bonus question - how would we modify this code to go the other way (from the percent string to a float)? – Shawn J. Molloy Feb 05 '13 at 20:18
  • 2
    @nocarrier You could do something like: `double value = 1.0 - ( double.Parse(percentage.Replace("Percentage is ", "").Replace("%", "")) / 100.0);` – Reed Copsey Feb 05 '13 at 20:29
3

Try:

(1 - currentPercentage) * 100

Example:

(1 - 0.95) * 100 = 0.05 * 100 = 5 [%]

Formatting:

string.Format("Percentage is {0}%", (1.0 - value) * 100);

Or the best one suggested by @Reed

mipe34
  • 5,596
  • 3
  • 26
  • 38
  • Why multiply * 100? The string formatting in .NET will handle percentage values correctly for you already... – Reed Copsey Feb 05 '13 at 20:00
  • And why not? I was first taking it from the perspective of math and forget about formatting potential of .NET :-). Anyway it is correct. – mipe34 Feb 05 '13 at 20:19
  • I voted you up - just wanted to point out that you don't need to do that in .NET, especially if the end goal is just formatting. (It also localizes better if you don't do it) – Reed Copsey Feb 05 '13 at 20:27
2

subract one (1) from your percent, then multiply by 100. 1-.5 = .5 = 50%. 1-.95 = .05 = 5%

Beth
  • 9,531
  • 1
  • 24
  • 43
  • Thank you Beth - your math here is correct, but @Reed beat you to the punch (and provided a great code sample). +1 for the effort. – Shawn J. Molloy Feb 05 '13 at 21:00