i'm trying to change a number inside a string. (i.e. 5.39) if the
number in position 2 is < = 4 then change it to 4 if its > = 5
change it to a 9.
how would i do this in vb 2005?
thanks.
Cor Ligthert[MVP] - 30 Jul 2007 23:15 GMT
jdrott,
You can use the If and the Mid command for this.
As you are familiar with VB6 it is the same as that.
Cor
> i'm trying to change a number inside a string. (i.e. 5.39) if the
> number in position 2 is < = 4 then change it to 4 if its > = 5
[quoted text clipped - 3 lines]
>
> thanks.
Jack Jackson - 31 Jul 2007 00:33 GMT
>i'm trying to change a number inside a string. (i.e. 5.39) if the
>number in position 2 is < = 4 then change it to 4 if its > = 5
>change it to a 9.
>
>how would i do this in vb 2005?
I doubt you really want to do what you say. I'm not sure which
character you think is in position 2, so I assumed it is the '3'. You
probably don't want to assume that the decimal point is the second
character, use _str.IndexOf("."c) to find out where it is. You
probably also don't want the second Substring call in the last line
which keeps the digits beyond the one that is changed.
Dim _chr As String
Dim _index As Integer = 2
Dim _str As String = "5.39"
If _str.Substring(_index, 1).ToInt32 <= 4
_chr = "4"
Else
_chr = "9"
End If
_str = _str.Substring(0, _index) + _chr + _str.SubString(_index + 1)
Göran Andersson - 31 Jul 2007 10:45 GMT
> i'm trying to change a number inside a string. (i.e. 5.39) if the
> number in position 2 is < = 4 then change it to 4 if its > = 5
> change it to a 9.
Why do you have the number in a string? As what you want is a numeric
operation, convert it to a number.
Dim price as Double
price = Double.Parse(theString)
price += 0.01
price *= 2
price = Math.Floor(price)
price /= 2
price -= 0.01
Use price.ToString("0.00") to create a string in the same format as the
original.

Signature
Göran Andersson
_____
http://www.guffa.com
Göran Andersson - 31 Jul 2007 11:47 GMT
>> i'm trying to change a number inside a string. (i.e. 5.39) if the
>> number in position 2 is < = 4 then change it to 4 if its > = 5
[quoted text clipped - 8 lines]
> price *= 2
> price = Math.Floor(price)
That should of course be Math.Ceiling.
> price /= 2
> price -= 0.01
>
> Use price.ToString("0.00") to create a string in the same format as the
> original.

Signature
Göran Andersson
_____
http://www.guffa.com