473,654 Members | 3,108 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 23709
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.

Similar topics

26
9670
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 " 4". Six spaces than the value of intQuantity. This is correct. But all the others end up being string objects of only 6 characters long (with the exception of strTotal). The left most positions of the string object are being padded with one...
3
2510
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 --> 3 0.0103 --> 4 0.00103 --> 5 0.000103 --> 6 0.0000103 --> 7
4
3579
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 c**d
8
6556
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 second iteration it should return the number 5, and so on. But for some reason on the first iteration returns the expected results. Each subsequent iteration returns the number plus 1. In order words, when I run the program I am getting: 7, 6, 4, and...
5
2936
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 <- Lengt = 5 Tnx for your attention hope you are capable of helping.
13
6178
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 ------------------ b += (float)(mode *val); On 32 bit(intel , vs 2003, C++), some watch variables are
2
6157
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
7196
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. the string has also to seem a random one and it cannot have an apparence of a sequence. My first approach is to do it with a decimal counter and find and use an encryption alghorithm that converts each 6 digit decimal number to a 6 digit...
2
5073
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 10(Value grows with decimal place)
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8594
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7307
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1596
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.