Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to remove decimals from string
Message
From
21/04/2010 03:02:19
 
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 2.0
Miscellaneous
Thread ID:
01461123
Message ID:
01461264
Views:
49
>>>>>>Using C#, how can I remove the decimals from a string? For example:
>>>>>>
>>>>>>"123.00" convert to "123"
>>>>>>
>>>>>>Thanks,
>>>>>
"123.00".Substring(0,s.IndexOf("."))
?
>>>>
>>>>
>>>>hmmmm
>>>
>>>Wha? Didn't work for you ? :-}
>>
>>
>>
>>If you define s before
>>
>>Also, when the string does not contain any dot you'll get a nice exception
>
>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 :-}



This works as well if you have an extension method Mod where the result is the same sign as the divisor ( a la foxpro)
http://en.wikipedia.org/wiki/Modulo_operation
// transform -1 to s.Length
string result = s.Substring(0, s.IndexOf('.').Mod(s.Length + 1));
		//______________________________________________________________________
		/// <summary>
		/// Performs a Remainder calculation where the result has the sign of dividend
		/// </summary>
		/// <param name="dividend"></param>
		/// <param name="divisor"></param>
		/// <returns></returns>
		public static Int32 Rem(this Int32 dividend, Int32 divisor)
		{
			return (dividend % divisor);
		}
		//______________________________________________________________________
		/// <summary>
		/// Performs a Remainder calculation where the result has the sign of dividend
		/// </summary>
		/// <param name="dividend"></param>
		/// <param name="divisor"></param>
		/// <returns></returns>
		public static Int64 Rem(this Int64 dividend, Int64 divisor)
		{
			return (dividend % divisor);
		}
		//______________________________________________________________________
		//______________________________________________________________________
		/// <summary>
		/// Performs a Modulo calculation where the result has the sign of divisor
		/// </summary>
		/// <param name="dividend"></param>
		/// <param name="divisor"></param>
		/// <returns></returns>
		public static Int32 Mod(this Int32 dividend, Int32 divisor)
		{
			return dividend.Rem(divisor)
					+ (Math.Sign(dividend) * Math.Sign(divisor) >= 0 ? 0 : divisor);
	
		}
		//______________________________________________________________________
		/// <summary>
		/// Performs a Modulo calculation where the result has the sign of divisor
		/// </summary>
		/// <param name="dividend"></param>
		/// <param name="divisor"></param>
		/// <returns></returns>
		public static Int64 Mod(this Int64 dividend, Int64 divisor)
		{
			return dividend.Rem(divisor)
					+ (Math.Sign(dividend) * Math.Sign(divisor) >= 0 ? 0 : divisor);
		}
		//______________________________________________________________________
	}
Gregory
Previous
Reply
Map
View

Click here to load this message in the networking platform