472,122 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

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 23295
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)

"Stefantastisk" <st***********@gmail.comwrote in message
news:11**********************@h54g2000cwb.googlegr oups.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().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

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, "sherifffruitfly" <sherifffruit...@gmail.comwrote:
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

26 posts views Thread by Adrian Parker | last post: by
3 posts views Thread by mrstephengross | last post: by
4 posts views Thread by nate | last post: by
8 posts views Thread by Candace | last post: by
5 posts views Thread by Stefantastisk | last post: by
13 posts views Thread by Shirsoft | last post: by
8 posts views Thread by Marc | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.