C # convert percentage to decimal number
This example describes how to convert percentages to Decimals in C. Share it with you for your reference. The specific analysis is as follows:
Recently, the percent to decimal function is required, and the percentage is in the string format (either with or without a percent sign ).
If it is decimal to percent conversion, It is easy. The numeric string in C # has a p.
But I do not know whether to convert percentages to decimals? After a brief look at MSDN, I did not find it ?).
Therefore, a method is directly implemented:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/// <Summary> /// Convert percentage to decimal number /// </Summary> /// <Param name = "perc"> Percentage value, which can be a pure numeric value or expressed by %, /// For example, 65 | 65% </param> /// <Returns> </returns> Public static decimal PerctangleToDecimal (string perc) { Try { String patt = @ "/^ (? <Num> [\ d] {1,}) (% ?) $ /"; Decimal percNum = Decimal. Parse (System. Text. RegularExpressions. Regex. Match (perc, patt). Groups ["num"]. Value ); Return percNum/(decimal) 100; } Catch { Return 1; } } |
The function is actually implemented, but it feels a little left-side.
If you are interested, give me some advice ~~
I hope this article will help you with C # programming.