• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

Easiest way to get number of decimals

Can somebody show me the easiest way to get to know the number of decimals from a double

Example:
0.1     I need to have the number 1 returned
0.06     I need to have the number 2 returned
0.004    I need to have the number 3 returned

Thanks in advance
0
liversen
Asked:
liversen
1 Solution
 
DhaestCommented:
Hmmm;... also interested .
I did it this way:

Dim val As String = System.Text.RegularExpressions.Regex.Replace((numToRound Mod 1).ToString(), "0*$", "") 
 
MessageBox.Show("numbers of decimals: " + (val.Length()-2)

Open in new window

0
 
MitzsCommented:
another method that produce the same result and will also handle where no. is not decimal.

Dim x As Decimal = 100.5
Dim y As Integer
y = x.ToString.IndexOf(".")
If y > 0 Then
MessageBox.Show("number of chars : " & x.ToString.Substring(y + 1).Length.ToString)
Else
MessageBox.Show("No decimals")
End If
0
 
liversenAuthor Commented:
Hmmm. I can't use the solution, but it is my own fault. I made a bad example
the values I have can also be like this:

34.01     Should return 2
345.003 should return 3
2343432.1234 should return 4

Meanwhile i have this idea: (See attached code)

Is there still a better way?



Dim strStepSize as string= dblStepSize  
Dim NoOfDecimals As String = _ 
strStepSize.Substring(strStepSize.IndexOf(".")).Length - 1

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
HooKooDooKuCommented:
Your proposed solution looks very elegant.  But depending upon what you are attempting to accomplish, I find the whole thing to be sort of problomatic.  Because when you are dealing with floating point numbers, there will be so many instances where the value stored in a double is going to be something like"1.233999999999998".
0
 
omegaomegaDeveloperCommented:
Hello, liversen,

Like HooKooDooKu says...   So I wonder about two things and maybe your answers will clarify things for us.
1. Why do you want to do this?  (I.e. what is the underlying objective?)
2. Why can't you use Mitzs' solution?

Cheers,
Randy
0
 
liversenAuthor Commented:

ad1: It's complicated. I need it for the Transact SQL rounding function which has a truncate parameter I need to set.

ad2: I can use Mitzs' solution. It was the solution from Dhaest: that I can not use.  Mitz just wrote his answer right before me and I did not see it at that time.

I'll go ahead and award the points to Mitzs

Thanks everyone
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now