extract int and fraction from string

  • Thread starter Patrick Sullivan
  • Start date
P

Patrick Sullivan

I am trying to extract two parts of a number from an array element. Numbers
are in the format of 1.10, 2.50, 11.10, etc. Floor and ceiling won't work
right because close to 1.00, I get a zero, and I need the actual integer
part and the fraction part as an int, too.. TIA

here is the data

fields(0) = "Alena Sobran"

fields(1) = "4/4/1977"

fields(2) = "9.00"

code

datetimepicker1.Value = fields(1).ToString ' works

nupdHour.Value = fields(2).ToString ' seems to work

nupdMinute.Value = fields(2).Substring(fields(2).IndexOf(".", 2)).ToString '
wrong value
 
J

Jim Underwood

Try placing fields(2) into a string variable before doing anythgin with it.
Do the same with the location fo the decimal point.

I see a few things in the code that could be problematic.

You are doing too many thigns in one step, and the code becomes difficult to
read and the mistakes hard to spot. Splitting this up into multiple lines
makes it easier to follow.

You are doing a substring on the database value before converting it to a
string. It has to be a string in order to use substring to begin with.
That said, I think the conversion will happen automatically, so this
shouldnt be the cause of your error, just a detail worth mentioning.

Your parenthesis is misplaced in the substring(indexof()) section of your
code. I think this is causing problems for you.

I'm not sure, as I always get confused with the index of substrings, but I
think you want to use indexof() +1 to start at the next character after the
decimal point.

Split up your functions as follows and check your values each step of the
way to confirm that each piece is workign exactly as expected. If you run
into a problem you can fixe one simple function instead of figuring which
part of a nested string function is failing.

dim strValue as string = fields(2).ToString
dim intDecPlace = strValue.Indexof(".")
nupdHour.Value = strValue
nupdMinute.Value = strValue.Substring(intDecPlace +1,2)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top