string work

  • Thread starter Thread starter jdrott1
  • Start date Start date
J

jdrott1

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.
 
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
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)
 
jdrott1 said:
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.
 
Göran Andersson said:
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)

That should of course be Math.Ceiling.
 
Back
Top