Click here to Skip to main content
14,238,047 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to find the length of particular double value in C#.

I'm novice, I dont know how to find the length of double value.

For example;

double sample1;
sample1=123;
int size;
size = //size of double value, that is 3.


how can i find the size of double value?

thanks in advance.
Posted
Comments
Rob Philpott 11-Jun-12 3:59am
   
By size, do you mean order-of-magnitude or length when expressed as a string? Not that it matters as you have both solutions below.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Use below code:
double sample1;
sample1 = 123;
int size;
size = sample1.ToString().Length;


Or
int size = de.ToString().Remove(de.ToString().IndexOf('.')).Length;
   
v3
Comments
VJ Reddy 11-Jun-12 4:09am
   
Good answer. 5!
Mehdi Gholam 11-Jun-12 4:26am
   
This will only work if the double does not have a decimal point and fractions.
♥…ЯҠ…♥ 11-Jun-12 7:46am
   
Thanks dude...!!!
Joezer BH 30-Jun-15 5:43am
   
5+
Rate this:
Please Sign up or sign in to vote.

Solution 1

Use this :
double de = 12345;
int len = (int)Math.Ceiling(Math.Log10(de));
   
Comments
VJ Reddy 11-Jun-12 4:08am
   
Good answer. 5!
Mehdi Gholam 11-Jun-12 4:25am
   
Thanks VJ!
♥…ЯҠ…♥ 11-Jun-12 7:46am
   
Thanks pal...
Joezer BH 30-Jun-15 5:43am
   
5+
Mehdi Gholam 30-Jun-15 5:56am
   
Cheers Joezer!
Member 12925579 28-Dec-16 9:54am
   
It doesnt work for anything after a decimal point. E.g. 0.2 gives a length of 0.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100