Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to remove decimals from string
Message
From
21/04/2010 12:56:01
John Baird
Coatesville, Pennsylvania, United States
 
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 2.0
Miscellaneous
Thread ID:
01461123
Message ID:
01461356
Views:
41
>>>>>>>
>>>>>>>OK. 'Twas drive-past coding. So's this:
string s = "123.00";
>>>>>>>string cut = s.IndexOf(".") > 0 ? s.Substring(0, s.IndexOf(".")) : s;
>>>>>>>
I'll do unit testing later :-}
>>>>>>
>>>>>>
>>>>>>I created an extension method called RemoveChar that takes the char to remove, ergo... string.RemoveChar('.')
>>>>>
>>>>>Will this make this string to be '12300' ?
>>>>Of course, you are an observant one. I didn't see he ware removing decimals, I thought he said decimal. If he want's to remove the decimals, you can create an extension method, remove decimals.....
>>>>
>>>>I'll post the method if you want..
>>>
>>>Yes, I'd like to see. Thanks in advance.
>>
>>
>>
>>class Program
>>	{
>>
>>		static void Main()
>>		{
>>			string s = "123.00";
>>
>>			string q = s.RemoveDecimals();
>>
>>			Console.WriteLine("{0} >> {1}", s, q);
>>			Console.ReadLine();
>>		}
>>		//______________________________________________________________________
>>	}
>>	public static class ExtensionMethods
>>	{
>>		public static string RemoveDecimals(this string s)
>>		{
>>                                     retVal= s;
>>                                     decimal val = 0;
>>
>>                                    if(s.Contains('.'))
>>                                    {
>>                                           if(decimal.TryParse(s, out val))
>>                                           {
>>                                                retVal = Convert.ToString(Convert.ToInt32(val));
>>                                           }
>>                                    }
>>
>>                                   return retVal;
>>                                }
>>	}
>>
Maybe this would be enough?
public static string RemoveDecimals(this string s)
>        {
>            decimal val = 0;
>            return (Decimal.TryParse(s, out val)) ? Convert.ToString(Convert.ToInt32(val)) : s;
>        }
Our coding standards don't allow us to place multiple calls in-line or to use IIF like constructs if that can be avoided. I get so used to typing it out so anyone can read it easily, I forget that it can be obfuscated :).
Previous
Reply
Map
View

Click here to load this message in the networking platform