This article describes the method of C# to convert percentage to decimals. Share it for your reference. The specific analysis is as follows:
Recently, the percentage to decimal function is needed, and the percentage is in string format (can be with or without a percent sign).
If it is a decimal turn percentage, it is simple. There is a p in the number format string in C#.
But I really don’t know if the percentage is converted into decimal? I took a brief look at MSDN and didn't find it (dreaming?).
Therefore, a method is directly implemented:
/// <summary> /// Convert percentages to decimals/// </summary> /// <param name="perc">Percent value, which can be purely a numeric value, or all with a % sign,/// For example: 65|65%</param>/// <returns></returns> public static decimal PerctangleToDecimal(string perc) { try { string patt = @"/^(?<num>[\d]{1,})(%?)$/"; decimal percNum = ((perc, patt).Groups["num"].Value); return percNum / (decimal)100; } catch { return 1; } }
The function has been implemented, but it feels a bit of a sideways.
Looking forward to the interested experts' advice~~
I hope this article will be helpful to everyone's C# programming.