Extract decimal from start of string

前端 未结 3 1689
旧巷少年郎
旧巷少年郎 2020-12-06 06:59

I have a string like 5.5kg or 7.90gram and I want to get 5.5 or 7.90 as a decimal value. How can I get such result in C#

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-06 07:07

    For your input format you can get decimal by this code

    var weight =Decimal.Parse( Regex.Match(input_string, "[0-9]*\\.*[0-9]*").Value);
    

    if your input string is in different format then you have to change the regex pattern.

提交回复
热议问题