Page **1** of **1**

### Splitting an Integral Number Into Two Parts

Posted: **Thu Jan 11, 2007 11:01 am**

by **CDRIVE**

Hello group,

I need to instruct QB to split an Integral (Integer ; Decimal) number into to parts. For example "1.75". I want to be able to work with the Integer and Decimal portions of this number as two independent numbers. Currently, my program does not create or call a file, or a record in a file. I would prefer to keep it that way if possible.

Currently this number is Dimensioned as: DIM answer AS INTEGER . The KeyWord "answer" is a number that is returned by an algebraic expression. Unfortunately this expression will return an "answer" value of "2" , as it rounds off the number "1.75".

Any help with this will be greatly appreciated.

Thank you,

CDRIVE

### Re: Splitting an Integral Number Into Two Parts

Posted: **Thu Jan 11, 2007 7:55 pm**

by **moneo**

CDRIVE wrote:Hello group,

I need to instruct QB to split an Integral (Integer ; Decimal) number into to parts. For example "1.75". I want to be able to work with the Integer and Decimal portions of this number as two independent numbers. Currently, my program does not create or call a file, or a record in a file. I would prefer to keep it that way if possible.

Currently this number is Dimensioned as: DIM answer AS INTEGER . The KeyWord "answer" is a number that is returned by an algebraic expression. Unfortunately this expression will return an "answer" value of "2" , as it rounds off the number "1.75".

Any help with this will be greatly appreciated.

Thank you,

CDRIVE

There might be a more scientific way of doing this, but here's two solutions:

First of all, you can't DIM answer as INTEGER and expect to retain any decimals places. You need to:

DIM answer AS SINGLE (or AS DOUBLE), depending on how much precision you want. I would just pick DOUBLE.

SOLUTION #1:

DIM answer AS DOUBLE

DIM ansint AS INTEGER 'The integer part of the answer

DIM ansdec AS DOUBLE ' The decimal part of the answer

answer = (algebraic expression)

ansint = FIX(answer)

ansdec = answer - ansint

SOLUTION #2:

DIM answer AS DOUBLE

DIM ansint AS INTEGER 'The integer part of the answer

DIM ansdec AS DOUBLE ' The decimal part of the answer

answer = (algebraic expression)

ans$=str$(answer)

decpt = instr(ans$,".") 'find position of any decimal point

if decpt = 0 then

ansint = answer

ansdec = 0

else

ansint = val(left$(ans$,decpt-1))

ansdec= val(mid$(ans$,decpt))

end if

Regards..... Moneo

Posted: **Fri Jan 12, 2007 2:41 am**

by **bungytheworm**

Solution one is something i tried with one program months ago. I also thought its simple and easy way but i had lots of problems with it. Not sure was it becuase i used qb with dosbox or what but results from double to integer were something weird pretty often. Double 1.67 could become like integers 1 and 20 or similar.

Solution two is bit more gumby way to solve this one but i would put my moneys on it if i would need safety and secure way to change double to integers.

Posted: **Fri Jan 12, 2007 6:30 pm**

by **Patz QuickBASIC Creations**

Why not just do...

Code: Select all

```
Answer=1.75
DecimalNum=Answer-INT(Answer)
IntegerNum=INT(Answer)
```

?

Posted: **Fri Jan 12, 2007 6:50 pm**

by **moneo**

Patz QuickBASIC Creations wrote:Why not just do...

Code: Select all

```
Answer=1.75
DecimalNum=Answer-INT(Answer)
IntegerNum=INT(Answer)
```

?

As E.K. pointed out, using DOUBLE (or SINGLE for that matter) will create floating point values with extra digits at the right end of the decimal value.

I apologize. E.K., for not having run some tests before posting my solutions. I did now, and I see what you mean.

So, Patz, your solution suffers from the same exact problems as my Solution #1. Apparently you didn't run any tests either.

My Solution #2, which E.K. calls "gumby" is the only one that solves the problem right now.

I also thought of performing rounding on the resultant floating point number produced bt Solution #1, but that's also risky.

Regards..... Moneo

Posted: **Fri Jan 12, 2007 7:40 pm**

by **Patz QuickBASIC Creations**

If you don't define a data type, doesn't QB automatically choose what data type would work best? If so, then I still don't understand the problem. But, like you said, I'm not able to run tests right now...

Posted: **Sat Jan 13, 2007 1:27 am**

by **CDRIVE**

Hi All,

Thank you for the replies and the time you spent trying to work this out. Of course you're correct, and I shouldn't attempt to DIM the value as an integer. I neglected to mention that this number starts life an improper fraction. As it turns out , the forward slash ( \ ) operator was just what I needed to manipulate a fraction that's being converted to an integral or decimal number, as it strips off the decimal value and returns only the Integer. Once that is accomplished, it was an easy matter to return the decimal portion also.

Print Num / Denom ( 11 / 4 ) returns 2 . 75

Print Num \ Denom ( 11\ 4 ) returns 2

BTW. How is the 'CODE" selection button used on the message board? I noticed some of the replies have the code in a little box.

Thanks again,

CDRIVE

Posted: **Sat Jan 13, 2007 5:37 am**

by **Nodtveidt**

Patz QuickBASIC Creations wrote:If you don't define a data type, doesn't QB automatically choose what data type would work best?

No, QB will make the variable of type SINGLE by default, unless you use one of the DEFtype functions, like DEFINT A-Z. The compiler isn't intelligent enough to select the "best" data type (hell, very few are, it's a wasted effort and encourages lazy-ass coding).

Posted: **Sat Jan 13, 2007 12:35 pm**

by **nkk_kan**

BTW. How is the 'CODE" selection button used on the message board? I noticed some of the replies have the code in a little box.

just click on Code Button then type out your code and click code button again which has now an asterisk on it too and it's done!

Posted: **Sat Jan 13, 2007 2:22 pm**

by **CDRIVE**

nkk_kan wrote:BTW. How is the 'CODE" selection button used on the message board? I noticed some of the replies have the code in a little box.

Just click on Code Button then type out your code and click code button again which has now an asterisk on it too and it's done!

Thanks for the tip. Here's some code in a box. While we're on the subject, what are List, List=, Img & URL? Does URL prevent a link from being broken in the mail?

Code: Select all

```
Print "Thank you!"
Print "You'r all a big help"
Print "CDRIVE"
```

Posted: **Sat Jan 13, 2007 10:38 pm**

by **moneo**

CDRIVE wrote:Hi All,

Thank you for the replies and the time you spent trying to work this out. Of course you're correct, and I shouldn't attempt to DIM the value as an integer. I neglected to mention that this number starts life an improper fraction. As it turns out , the forward slash ( \ ) operator was just what I needed .......

Just for the record:

/ is a forward slash

\ is a backslash

If you intend using INTEGER DIVISION using the backslash, I recommend reading the manual or Help on the subject because my manual states:

" Before integer division is performed, operands are rounded to integers or long integers....". Anytime QB says that rounding is performed, beware, becuase it doesn't declare what method of rounding is used, which may not be what you assume. Also, do some detail testing.

Regards..... Moneo

Posted: **Mon Jan 15, 2007 9:34 am**

by **CDRIVE**

Thank you for the infomation regarding the use of operands and their pitfalls. Actually this operand (\) is working well in my code. The only time it throws a curve, is if the user inputs something other than an integer. Which they shouldn't do, as they are instructed to enter the Numerator first and then the Denominator. That said, I'm experimenting with the "FIX (N)" function. It seems to have promiss as it does it's work while the number is in Integral (integer + decimal) form instead of Fractional. QB45 has a few examples of its use, but the best example is found listed under the "INT" function. This example demonstrates the different results between INT(N), CINT(N) & FIX(N).

BTW, I always get "forward (/) & Back (\) slash" backwards! Slashexia is a little know disease!

CDRIVE

Posted: **Mon Jan 15, 2007 7:14 pm**

by **moneo**

CDRIVE wrote:.....

I'm experimenting with the "FIX (N)" function. It seems to have promiss as it does it's work while the number is in Integral (integer + decimal) form instead of Fractional. QB45 has a few examples of its use, but the best example is found listed under the "INT" function. This example demonstrates the different results between INT(N), CINT(N) & FIX(N).

CDRIVE

Beware of using the example listed under the "INT", because it is not complete enough to give you a real example of how INT and CINT work with other numbers. However, the FIX examples are fine 'cause FIX just truncates.

If you look at the INT results, it seem to just be truncating, except for negative numbers where it bumps up to the next absolute value, like

-99.7 gives -100 instead of 99. This is because of th tricky definition of INT which says "returns the largest integer less than or equal to the numeric-expression." Whatever.

The CINT is a real piece of work. It rounds the number in a special way. I've figured out, by much testing, that it performs what is known as Bankers Rounding. This is tricky stuff. Basically, if the number ends in .5 and is even, it DOES NOT round it up. If the number ends in .5 and is odd, then it rounds it.

It handles positive and negative in the same manner, which is know as a symmetric method. Not like INT.

Example with numbers that are not on the INT list:

98.3 gives 98 (normal rounding)

98.5 gives 98 (.5 and integer portion is even, no rounding)

98.7 gives 99 (normal rounding)

The following numbers were on the INT list:

99.3 gives 99 (normal rounding)

99.5 gives 100 (.5 and integer portion is odd, so it rounds up)

99.7 gives 100 (normal rounding)

If you were to add the above 98.x numbers to the INT example list, you would have a complete example.

BTW, during my testing, I discovered the following:

N% = X! \ Y!

is the same as:

N% = X! / Y!

because N is an integer and the result of the regular divide (X! / Y!) is truncated to be able to stuff the result into the this integer. Of course, this is only true when N is an integer.

Regards..... Moneo

Posted: **Tue Jan 16, 2007 11:02 pm**

by **CDRIVE**

Thank you. "FIX(N) looks like the way to go for me. It's the only function, of the three I mentioned, that doesn't change the integer.

CDRIVE