473,714 Members | 2,574 Online

# Length of digit after decimal point

Hey there,

Anyone knows a clever method for knowing the length of the digit after
a decimal point in a standard C# decimal value, WITHOUT use of any
string formatting.

Example:
5231,12231 <- Lengt = 5

Tnx for your attention hope you are capable of helping.

Kind regards
Stefan

Nov 14 '06 #1
7 23725
Stefantastisk,

I'd suggest to stick working with strings. The number of digit after the
decimal point could be infinite; take for example 10/3 or PI. The things
gets even worse when you add the errors that floating point types add.
--
Stoitcho Goutsev (100)

"Stefantast isk" <st***********@ gmail.comwrote in message
news:11******** **************@ h54g2000cwb.goo glegroups.com.. .
Hey there,

Anyone knows a clever method for knowing the length of the digit after
a decimal point in a standard C# decimal value, WITHOUT use of any
string formatting.

Example:
5231,12231 <- Lengt = 5

Tnx for your attention hope you are capable of helping.

Kind regards
Stefan

Nov 14 '06 #2
Stefan,

Try:

decimal myDec = 5231.12231M;
int intLength = (myDec % 1).ToString().L ength - 2;

It still has the ToString(), but this does nothing more than save you a
loop that multiplies by 10 and adds to a counter. You'll have to check
for zero conditions through the maximum length of a decimal, to make
sure you don't stop counting before the end if your number is something
like 1.000000005.
Stephan

Stefantastisk wrote:
Hey there,

Anyone knows a clever method for knowing the length of the digit after
a decimal point in a standard C# decimal value, WITHOUT use of any
string formatting.

Example:
5231,12231 <- Lengt = 5

Tnx for your attention hope you are capable of helping.

Kind regards
Stefan
Nov 14 '06 #3
Stoitcho Goutsev (100) wrote:
I'd suggest to stick working with strings. The number of digit after the
decimal point could be infinite; take for example 10/3 or PI. The things
gets even worse when you add the errors that floating point types add.
That's incorrect:

1) The number of digits after the decimal point cannot be infinite.
It's guaranteed to be 29 or less.

2) The decimal data type was created to eliminate certain types of
error that you'd get in a float (single) or double. If Stefan is
correctly using a decimal, there should be no error. If you're
expressing 10/3 or pi, decimal most likely is the wrong data type.
Stephan

Nov 14 '06 #4

Stefantastisk wrote:
Hey there,

Anyone knows a clever method for knowing the length of the digit after
a decimal point in a standard C# decimal value, WITHOUT use of any
string formatting.

Example:
5231,12231 <- Lengt = 5

Tnx for your attention hope you are capable of helping.

Kind regards
Stefan
It's *intrinsically* a string issue. As far as numbers/math is
concerned, *every* decimal (i.e. every real number) has the same number
of digits to the right of the decimal point: a countable infinity of
them.

The only way to get a different count is to consider the number as a
string, and apply some convention or another. E.g., there could
hypothetically be a culture somewhere whose practice is to never write
a number whose rightmost digit is '7'. This of course doesn't result in
the loss of any numerical expressive capability, but it *does* change
the count you're talking about. (Just to highlight the fact that both
strings and conventions are the heart of the matter here.)

Nov 14 '06 #5
It's *intrinsically* a string issue. As far as numbers/math is
concerned, *every* decimal (i.e. every real number) has the same number
of digits to the right of the decimal point: a countable infinity of
them.
Your statement is true, but it's not completely applicable to this
situation. Yes, every real number has an infinite number of decimal
places, but computers don't ever deal with pure real numbers. The
decimal data type represents a particular subset of the set of all real
numbers. Stefan's question was about the decimal data type, not about a
real number.

Nevertheless, I maintain and agree that string handling is the best way
to solve this problem.
The only way to get a different count is to consider the number as a
string, and apply some convention or another. E.g., there could
hypothetically be a culture somewhere whose practice is to never write
a number whose rightmost digit is '7'. This of course doesn't result in
the loss of any numerical expressive capability, but it *does* change
the count you're talking about. (Just to highlight the fact that both
strings and conventions are the heart of the matter here.)
I was making the (generally implicit) assumption that he's using a
standard westen culture.
Stephan

Nov 14 '06 #6

ssamuel wrote:
>
I was making the (generally implicit) assumption that he's using a
standard westen culture.
Sorry - I had attempted to be clear that the example was *only* meant
to highlight the fact that strings are the *heart* of the matter. I
didn't mean to suggest that he *really* might be using a different
string convention.

Nov 14 '06 #7
sherifffruitfly , ssamiel and Stoitcho Goutsev,

Thank you so much for your reactions, I am also concluding that there
is no slick way to avoid the use of strings. My only worry regarding
the use strings is the cultural differences in string handling, but
that is a another subject :-)

Take care u guys, and again, thank you.

Regards,
Stefan

On 14 Nov., 19:50, "sherifffruitfl y" <sherifffruit.. .@gmail.comwrot e:
ssamuel wrote:
I was making the (generally implicit) assumption that he's using a
standard westen culture.Sorry - I had attempted to be clear that the example was *only* meant
to highlight the fact that strings are the *heart* of the matter. I
didn't mean to suggest that he *really* might be using a different
string convention.
Nov 15 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.