473,395 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 23649
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Adrian Parker | last post by:
I'm using the code below in my project. When I print all of these fixed length string variables, one per line, they strings in questions do not properly pad with 0s. strQuantity prints as " ...
3
by: mrstephengross | last post by:
Hi all... How can I find out the number of significant digits (to the right of the decimal place, that is) in a double? At least, I *think* that's what I'm asking for. For instance: 0.103 -->...
4
by: nate | last post by:
Hey everyone, I am trying to figure out what is the largest integer I can. Lets say for 400 megabytes of memory at my disposal. I have tried a few things c = 2**1000000 d = 2**2000000 print...
8
by: Candace | last post by:
I am using the following code to pick off each digit of a number, from right to left. The number I am working with is 84357. So for the first iteration it should return the number 7 and for the...
5
by: Stefantastisk | last post by:
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 <-...
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
2
by: Nasir Rehaman | last post by:
Salam i want to display numbers with only two digit after decimal point using c#, but coud'nt success.Please any body help me.
8
by: Marc | last post by:
Hi all, I have to generate and send to a printer many 6 digit alphanumeric strings. they have to be unique but I cannot check in a database or something like that if it have already been printed....
2
by: Jcan | last post by:
HI I have a 10 digit ASCII array: char Arr = {'1','2','3','4','5','6','7','8','9','1'} I have to convert the the array to decimal. I do this by subtracting 0x30 and multiplying with...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.