Percent float conversion with weird sql formatting

I have an SQL table with a column with a Float data type. There is an external application that writes percentages to this column, and I read them and show them in a web application.

I understand how to convert decimal numbers to percent; I would usually multiply by 100, but that's a little different.

The values ​​in the database look like this:

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

I 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, for example

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

Articles on formatting, rounding, and converting percentages in C #:

Convert Percentage to Nearest Share , Working Percentage in C # , http://www.dotnetperls.com/percentage ,

But for my life I can’t find a mathematical calculation or a built-in C # converter to give me the result that I need.

It seems that what I need to do is get 0.96 digits, which will be subtracted by some number, and then, in turn, multiply this by 100, but 0.5 out of 50% always throw away my math ....

This seems to be a routine appeal; What am I missing here?

UPDATE

Here is the solution I ended up with that 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 completeness, here is a list of interesting resources on Decimals, Percentages, Doubles, and Conversions in C #:

A +

Worker percent in C #
Decimal value format for percentage values?
Convert Percentage to Nearest Faction
http://www.dotnetperls.com/percentage
.NET: decimal rounded string
C # Is there a built-in function to convert a formatted string to a number?

A -

Percent decimal format with specific decimal places
Convert decimals to percentages or shift decimals. how
How to convert percentage string to double?

B +

two ways to display decimal places
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

+4
source share
3 answers

I believe that you need:

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

If you just want a number:

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

Or a number without decimal precision:

 string percentage = (1.0 - value).ToString("P0"); 
+5
source

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 suggested by @Reed

+3
source

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

+2
source

All Articles