0

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))
sinDizzy
  • 1,300
  • 7
  • 28
  • 60

2 Answers2

0

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()
Ali Alavi
  • 2,367
  • 2
  • 18
  • 22
  • ok am testing now. It works with some tweaks. Its important to add the intended format in Dim numAsStr As String = num.ToString("0.000") or whatever that may be. – sinDizzy Jan 21 '14 at 20:33
  • Also to deal with the format being "0" or "0." where the result is an integer I split up into two cases. If numAsStr.IndexOf(".") = -1 Then Dim result As Double = minVal + 1 and then the other case is as you wrote it. – sinDizzy Jan 21 '14 at 20:34
  • @sinDizzy please look at the edited answer. It is completely updated. – Ali Alavi Jan 21 '14 at 20:35
  • ok let me take a look but doesn't that just omit the formatting like "0.000". – sinDizzy Jan 21 '14 at 20:37
  • You need to take care of formatting when converting it to a String, e.g. Console.WriteLine (result.ToString("0.000")) – Ali Alavi Jan 21 '14 at 20:41
  • @sinDizzy Okay I think by 0.000 you mean cases like 10.100. In this case the original answer is the best choice I think. Especially since some rounding errors in Log lead to wrong results in some cases. – Ali Alavi Jan 21 '14 at 20:53
  • the log function craps when the value is zero. i think it was working how you suggested one revision back with my tweaks. – sinDizzy Jan 21 '14 at 20:54
  • well yes the user will ionput a format string like "0.0" or "0.00000". My algorithm needs to adjust for that. looking at your prior suggestion that works the best. let me test a bit more. – sinDizzy Jan 21 '14 at 20:56
  • some tweaks I made and this works. first I added the user specific format and second I split into the case where the result has no decimal point or is an integer. 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)) – sinDizzy Jan 21 '14 at 21:23
  • That works for negative and positive numbers and for almost every formatting string I have input. It can be taken further since the point may not be the decimal separator like in some European countries but for me its only for internal usage. – sinDizzy Jan 21 '14 at 21:24
0

Use "ON ERROR RESUME NEXT" construct for this problem.