C#實現百分比轉小數的方法

來源:互聯網
上載者:User

   本文執行個體講述了C#實現百分比轉小數的方法。分享給大家供大家參考。具體分析如下:

  近日需要用到百分比轉小數功能,而且百分比是字串格式(可以帶或不帶百分比符號)。

  如果是小數轉百分比那就簡單了,C#裡數字格式字串有個 p 。

  但是百分比轉小數還真不知道有沒有?簡單看了一下MSDN,沒有發現(眼花?)。

  因此,直接搞了一個方法來實現:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 /// <summary> /// 將百分比轉換成小數 /// </summary> /// <param name="perc">百分比值,可純為數值,或都加上%號的表示, /// 如: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; } }

  功能倒是實現了,但是感覺有點旁門左道。

  期待感興趣的高人指點一二~~

  希望本文所述對大家的C#程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.