Length of digit after decimal point

  • Thread starter Thread starter Stefantastisk
  • Start date Start date
S

Stefantastisk

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
 
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.
 
Stefan,

Try:

decimal myDec = 5231.12231M;
int intLength = (myDec % 1).ToString().Length - 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
 
Stoitcho said:
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
 
Stefantastisk said:
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.)
 
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
 
ssamuel said:
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.
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top