By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,268 Members | 1,371 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,268 IT Pros & Developers. It's quick & easy.

Double data type count number of decimal places?

P: n/a
I require a function that takes a double as a parameter and returns the number of decimal places. What is the most efficient way in c# to find out the number of decimal places

I have written a function that does it by converting the double to a string. Is there a better way

public static int CountDecimalPlaces(double double1)

return double1.ToString().Substring(double1.ToString().In dexOf(".")+1).Length
Thank yo

Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
decimals=0;
while ((int)double1 % 10 ==0)
{
double1*=10;
decimals++;
}

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
"android" <an*****@newsgroups.nospam> schrieb im Newsbeitrag
news:20**********************************@microsof t.com...
I require a function that takes a double as a parameter and returns the number of decimal places. What is the most efficient way in c# to find out
the number of decimal places?
I have written a function that does it by converting the double to a string. Is there a better way?
public static int CountDecimalPlaces(double double1)
{
return double1.ToString().Substring(double1.ToString().In dexOf(".")+1).Length; }

Thank you

Nov 16 '05 #2

P: n/a
Hi android,

Based on my understanding, you want to get the decimal length of your
double value.

As I think your solution of using ToString should be a good way, and can be
good to read.

Use other solution, I think you may use some number operation, which may
need loop. You may also get the correct result, but they will have less
readability.

For performance issue, I think this will not have much performance problem,
other solution may not be better than yours.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 16 '05 #3

P: n/a
android <an*****@newsgroups.nospam> wrote:
I require a function that takes a double as a parameter and returns
the number of decimal places. What is the most efficient way in c# to
find out the number of decimal places?


That's not really a particularly useful measure - a double value is
likely to have lots of decimal places which may not be useful to the
caller. For instance, the exact value of the double given by the
literal 0.1 is

0.100000000000000005551115123125782702118158340454 1015625

Now, chances are the caller is really only interested in 1 decimal
place - but you can't actually tell.

Using ToString("R") will give you (I believe) the shortest form of the
double which can always be parsed back to the same double, which is one
measure which might be useful to you.

See http://www.pobox.com/~skeet/csharp/floatingpoint.html and
http://www.pobox.com/~skeet/csharp/decimal.html for more information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.