VB2010 I have a user form where the user inputs a number format. The routine then cycles through a list of number pairs and displays them in a list of categories:
User format "0.00"
0.00 - 164.04
164.04 - 410.10
410.10 - 820.21
What I am trying to do is to increment the first value by one digit so there is no overlap. something like:
0.00 - 164.04
164.05 - 410.10
410.11 - 820.21
I am trying to make it so it works with any number format the user inputs like "0.000" or "0.0". What I currently have is (example for value 164.04)
1. Convert the value to a string "164.04"
2. Take the right most character "4" and convert to an integer 4
3. Increment the integer value by 1 to get 5
4. Take the characters in the string from step #1 except the last and then append
the integer from Step #3 as a string to get "164.05".
Seemed to work in my VB6 program but wanted to see if anyone had any better ideas. I also don't think i accounted for the last digit being a 9.
Update: based on the suggestions below what ended up working for positive and negative numbers and integers and floats was the following:
Dim p As Integer
Dim numAsStr As String = num.ToString(fmt)
If numAsStr.IndexOf(".") = -1 Then
p = 0
Else
p = numAsStr.Length - numAsStr.IndexOf(".") - 1
End If
Dim result as Double = ((num* (10 ^ p) + 1.0) / (10 ^ p))
Here is the algorithm:
1.Find decimal points (p)
2.multiply the number by 10^p, increase it by one, divide it back by 10^p
Dim numAsStr As String = num.ToString()
Dim p As Integer = numAsStr.Length - numAsStr.IndexOf(".") - 1
Dim numInt as Integer = 10^p * num
Dim result as Double = ((10^p *num + 1.0) / 10^p).ToString()