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);
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