473,587 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

extract int and fraction from string

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).ToStr ing ' works

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

nupdMinute.Valu e = fields(2).Subst ring(fields(2). IndexOf(".", 2)).ToString '
wrong value

----
IOW, nupdHour should be 9 and nupdMinute should be 0 or 00

nupdMinute keeps returning 34

--

Patrick Sullivan, AA-BA, BA-IT
Nov 23 '05 #1
1 2085
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(index of()) 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).ToStr ing
dim intDecPlace = strValue.Indexo f(".")
nupdHour.Value = strValue
nupdMinute.Valu e = strValue.Substr ing(intDecPlace +1,2)

"Patrick Sullivan" <ps****@eatel.n et> wrote in message
news:RJ******** ************@ea tel.net...
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).ToStr ing ' works

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

nupdMinute.Valu e = fields(2).Subst ring(fields(2). IndexOf(".", 2)).ToString ' wrong value

----
IOW, nupdHour should be 9 and nupdMinute should be 0 or 00

nupdMinute keeps returning 34

--

Patrick Sullivan, AA-BA, BA-IT

Nov 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2280
by: amarok | last post by:
Hello all. I'm a Software Engineering student, and I'm attempting to write a program in Java that does as follows: UML for the class: Fraction() Fraction(numerator: int) Fraction(numerator: int, denominator: int) Fraction(value: String) add(frac: Fraction): Fraction subtract(frac: Fraction): Fraction
6
13400
evilmonkey
by: evilmonkey | last post by:
I am very new to programming as well as Java and this is my first post so please forgive me if this is not quite posted correctly. My Problem is that I have only been using scanner to get user input into most of the exercises I have done. This exercise is asking for a user to enter two fractionslike "1/3" or "5/8". Scanner doesn't work and I...
1
2197
by: d0ugg | last post by:
Hi, I'm did a fraction program for one of my programming classes and it did compile, however when I'm running the program it crashes for some reason that I do not know. // fraction.cpp #include <iostream> #include <string>
2
2999
by: d0ugg | last post by:
Hi, I'm doing a FRACTION program for one of my Programming classes and I'm getting some errors that I can't figure it out. Here is the Assignment: 1. Convert the fraction structure into a class named fraction declared in a file named fraction.h, with two private data members: an int numerator and an int denominator. 2. Convert the four...
4
14443
by: d0ugg | last post by:
Hello everyone, I'm creating a program that it is suppose to add, subtract, multiply and also divide fractions and after that, the result has to be reduced to the lowest terms. However, I'm not sure how the algorithm of reducing fractions works. Here is part of my code: //Header File
4
1866
by: Semajthewise | last post by:
Hi All For those of you that helped me with the questions on this... Thank you! I believe I have solved all the bugs in the code and wanted to post the final product. This is a set of code to solve basic frational arithmatic. using only 3 textboxes. Allowing the user to enter any type of fraction and get a proper reduced fraction answer. Feel Free...
1
10205
by: sony.m.2007 | last post by:
Hi, I have a decimal value and i need to take the digits after decimal places(fraction part only). Is there any simple functions available other than using string functions(searching for '.' and take the string after that) decimal decimalValue=12.745 I need to take .745 as another decimal value only from the decimalValue Thanks,
45
4402
by: Dennis | last post by:
Hi, I have a text file that contents a list of email addresses like this: "foo@yahoo.com" "tom@hotmail.com" "jerry@gmail.com" "tommy@apple.com" I like to
10
5308
by: Jason | last post by:
I'm making a program that will convert decimal inputs (in this case, in inches) and output a fractional answer. At the moment, I'm only able to output the fractional answer in three parts: A whole number text box, a numerator text box, and a denominator text box. I realize that this is probably the closest I'll get to having fractions...
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.