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#
5.5kg
7.90gram
5.5
7.90
here is a completely different approach
string oldstr = "1.7meter"; Char[] strarr = oldstr.ToCharArray().Where(c => Char.IsDigit(c) || Char.IsPunctuation(c)).ToArray(); decimal number = Convert.ToDecimal( new string(strarr));