Author |
Topic: Rounding numbers (Read 2267 times) |
|
CharlesB
New Member
member is offline


Gender: 
Posts: 46
|
 |
Re: Rounding numbers
« Reply #11 on: Apr 10th, 2015, 9:32pm » |
|
Richard, not to take advantage of your time and kindness, but the following situation is another reason why I wanted rounded numbers and could not figure how to use the PRINT FNusing function as a solution.
I could think of no other way to produce a string with the proper "Price" value.
The code below is obviously wrong, but it is just one example of my frustrations.
Code:c$ = " "+ str$(Quantity%)+" "+ chr$(126)+ " ft "+ SelectedTubing$ + fnusing($##.##,Price) + "/ft. 20 ft lengths"
I was able to use the help of Eddy and D to produce a rounded value for "Price," convert it to a String variable, and thus be able to move on. At least the string would make sense.
Code:c$ = " "+ STR$(Quantity%)+" "+ CHR$(126)+ " ft "+ SelectedTubing$ + "$" +FixedPrice2$+ "/ft. 20 ft lengths"
The only problem was that their functions could produce a number as 40.9, where I would want a 40.90. This was simple to fix with some string manipulations, but not easy for me. This will really humble you!
In such a situation did I have a better alternative with PRINT FNusing?
|
| « Last Edit: Apr 10th, 2015, 9:34pm by CharlesB » |
Logged
|
|
|
|
Richey
New Member
member is offline


Gender: 
Posts: 35
|
 |
Re: Rounding numbers
« Reply #12 on: Apr 10th, 2015, 10:24pm » |
|
on Apr 9th, 2015, 04:55am, CharlesB wrote:I'm sure that this is an elementary question, and surely, I have searched the help files many times for this simple answer, but I can't find it.
I simply want to round off a number to two decimal places. I am aware of the PRINT FNUSING function and use it, but I am looking for something like USING$( ) that will actually convert (round off) a number.
So, may someone assist me to say, convert 34.8456123 to 34.85
Thank you, and please point me to the help files where I can learn to do this. Charles |
|
As someone who is still pretty much a novice, I tried to see if there was another way of doing this by practising what I have learnt about string slicing using MID$.
Here is my inelegant and clumsy effort. Apologies in advance for any errors / dodgy approaches:
Code:
INPUT "Floating Point number to be truncated ";test
INPUT "Rounded to how many decimal places ";places
count=0
convert$=STR$(test)
LenOfString=LEN(convert$)
FOR I=1 TO LenOfString
a$=MID$(convert$,I,1)
count=count+1
IF a$="." THEN EXIT FOR
PRINT a$;
NEXT I
PROC_decimal
END
DEF PROC_decimal
PRINT MID$(convert$,count,places+1)
ENDPROC
|
|
Logged
|
|
|
|
rtr2
Guest
|
 |
Re: Rounding numbers
« Reply #13 on: Apr 10th, 2015, 10:29pm » |
|
on Apr 10th, 2015, 9:32pm, CharlesB wrote:| I wanted rounded numbers and could not figure how to use the PRINT FNusing function as a solution. |
|
You seem to have got in your mind the false notion that FNusing() must be used in conjunction with PRINT. I wonder if this stems from your background with QBASIC (and other similar BASICs) in which it is indeed true that, stupidly, PRINT USING is the only usage.
But in BBC BASIC (with the FNUSING library), and also in Liberty BASIC, USING (or FNusing) is a regular function which can be used anywhere in your program; it is not at all tied to PRINT. Indeed a good way of thinking about it is that FNusing() has the same basic functionality as STR$(): they both take a numeric value and convert it into a decimal string.
The difference between STR$() and FNusing() is simply in how the formatting of the decimal number is controlled. With STR$() it is controlled using the special @% variable, whereas with FNusing() it is controlled by passing a special formatting string as the first parameter. But fundamentally the two functions do the same thing:
Code: INSTALL @lib$+"FNUSING"
a = 34.8456123
REM STR$ method (formatting controlled by @%):
@% = &102020A
a$ = STR$(a)
PRINT a$
REM FNusing method (with a formatting string):
a$ = FNusing("##.##", a)
PRINT a$
Quote:| The code below is obviously wrong, but it is just one example of my frustrations. |
|
You don't actually say what your "frustration" is. In your code you use both STR$() and FNusing(), which is absolutely fine, but it is worth bearing in mind that they are effectively doing the same thing, just with different ways of controlling the formatting.
Putting your code in the context of an entire program it seems to work perfectly well, so again I don't see where the "frustration" arises:
Code: INSTALL @lib$+"FNUSING"
Quantity% = 3
SelectedTubing$ = "3/4 inch "
Price = 23.3456789
c$ = " " + STR$(Quantity%) + " " + CHR$(126) + " ft " + \
\ SelectedTubing$ + FNusing("$##.##", Price) + "/ft. 20 ft lengths"
PRINT c$ I certainly don't think the functions offered by Edja and DDRM have any place here at all. Their contributions were well-meaning but have sent you in the wrong direction.
Richard.
|
|
Logged
|
|
|
|
CharlesB
New Member
member is offline


Gender: 
Posts: 46
|
 |
Re: Rounding numbers
« Reply #14 on: Apr 10th, 2015, 11:55pm » |
|
Thank you again Richard. This is very helpful and I've learned a lot. My "frustrations" were just in my own inability to look at your help files (well written indeed), but to make proper sense of them so that I could correctly use them without introducing errors . . . again and again."
At first, I did not even understand that the FNusing could "operate" outside the realm of "PRINT". I mean, this is where I am.
Now I have something that I understand to work with, so again, thank you!
|
| « Last Edit: Apr 10th, 2015, 11:59pm by CharlesB » |
Logged
|
|
|
|
Edja
Developer
member is offline


Posts: 60
|
 |
Re: Rounding numbers
« Reply #15 on: Apr 11th, 2015, 12:36pm » |
|
Quote:| I certainly don't think the functions offered by Edja and DDRM have any place here at all. Their contributions were well-meaning but have sent you in the wrong direction. |
| True. Your further questions "unfolded" an objective that was clearly different. Eddy
|
|
Logged
|
|
|
|
rtr2
Guest
|
 |
Re: Rounding numbers
« Reply #16 on: Apr 11th, 2015, 1:08pm » |
|
on Apr 11th, 2015, 12:36pm, Edja wrote:| Your further questions "unfolded" an objective that was clearly different. |
|
The OP was no doubt trying to be helpful by distilling his problem down to what he thought were the basics. I encourage that, but there is a danger - as happened here - that the true objective will not be conveyed accurately.
Part of the problem is that the underlying issues are mathematical rather than specifically to do with programming. It's fundamental that 'rounding to two decimal places' has no well-defined meaning when applied to a number represented in binary floating-point. Indeed the concept of 'decimal places' only really applies to decimal numbers!
That is precisely why the built-in rounding functionality in BBC BASIC (in STR$ or in FNusing, which uses STR$ to do the work) happens only when converting a binary number to a decimal number.
Richard.
|
|
Logged
|
|
|
|
|