Count Decimals
-
Hi I've been searching through multiple articles on this topic, however, what I'm not finding is a simple way to count decimals. I simply want to count how many decimals are in the string, unfortunately everything in regex seems to involve replacing the decimals...and regex seems really heavy way of doing something that should be simple. Any thoughts on this topic?
----------------------------------------------------------- Completion Deadline: two days before the day after tomorrow
-
Hi I've been searching through multiple articles on this topic, however, what I'm not finding is a simple way to count decimals. I simply want to count how many decimals are in the string, unfortunately everything in regex seems to involve replacing the decimals...and regex seems really heavy way of doing something that should be simple. Any thoughts on this topic?
----------------------------------------------------------- Completion Deadline: two days before the day after tomorrow
jgasm wrote:
I simply want to count how many decimals are in the string, unfortunately everything in regex seems to involve replacing the decimals...and regex seems really heavy way of doing something that should be simple.
If you know the string always has a decimal:
Int32 decimalCount = text.Split('.')[1].Length
If it doesn't always have a decimal, it gets much more complicated :):
Int32 decimalCount = text.IndexOf('.') > -1 ? text.Split('.')[1].Length : 0;
Share and enjoy. Sean
-
Hi I've been searching through multiple articles on this topic, however, what I'm not finding is a simple way to count decimals. I simply want to count how many decimals are in the string, unfortunately everything in regex seems to involve replacing the decimals...and regex seems really heavy way of doing something that should be simple. Any thoughts on this topic?
----------------------------------------------------------- Completion Deadline: two days before the day after tomorrow
Count significant decimal places? This is rather rough:
[System.STAThreadAttribute]
static void Main(string[] args)
{
int d ;args \[ 0 \] = args \[ 0 \].Trim( new char\[\] { '0' } ) ; d = args \[ 0 \].IndexOf ( '.' ) ; if ( d >= 0 ) { d = args \[ 0 \].Length - d - 1 ; } else { d = 0 ; } System.Console.WriteLine ( d ) ;
}
-
Count significant decimal places? This is rather rough:
[System.STAThreadAttribute]
static void Main(string[] args)
{
int d ;args \[ 0 \] = args \[ 0 \].Trim( new char\[\] { '0' } ) ; d = args \[ 0 \].IndexOf ( '.' ) ; if ( d >= 0 ) { d = args \[ 0 \].Length - d - 1 ; } else { d = 0 ; } System.Console.WriteLine ( d ) ;
}
Thank you so much for the reply. :) However, Sean's answer was exactly what I was looking for.
----------------------------------------------------------- Completion Deadline: two days before the day after tomorrow
-
Hi I've been searching through multiple articles on this topic, however, what I'm not finding is a simple way to count decimals. I simply want to count how many decimals are in the string, unfortunately everything in regex seems to involve replacing the decimals...and regex seems really heavy way of doing something that should be simple. Any thoughts on this topic?
----------------------------------------------------------- Completion Deadline: two days before the day after tomorrow
A similar question was asked in the microsoft.public.dotnet.languages.vb newsgroup two days ago. Here's the code that I suggested, converted to C#, and assuming that the decimal separator is a period:
int pos = text.IndexOf('.');
int decimalCount;
if (pos == -1) {
decimalCount = 0;
} else {
decimalCount = value.Length - pos - 1;
}It's about ten times faster than splitting the string. :) If you want it in few lines:
int pos = text.IndexOf('.');
int decimalCount = (pos == -1) ? 0 : value.Length - pos - 1;--- single minded; short sighted; long gone;
-
Thank you so much for the reply. :) However, Sean's answer was exactly what I was looking for.
----------------------------------------------------------- Completion Deadline: two days before the day after tomorrow
If you don't need the results of the Split for something else then mine is likely more efficient. Here it is with the Conditional Operator:
int d ;
args [ 0 ] = args [ 0 ].Trim( new char[] { '0' } ) ;
System.Console.WriteLine
(
( d = args [ 0 ].IndexOf ( '.' ) ) == -1 ? 0 : args [ 0 ].Length - d - 1
) ;