Programming => Example Code => General => Topic started by: Arnold on March 08, 2015, 11:22:03 AM
Title: Question about round()
Post by: Arnold on March 08, 2015, 11:22:03 AM
Hi Charles,
for rounding a fractionial decimal number I currently use a macro:
Code: OxygenBasic
macro roundto(x,d) round(x*pow(10,d))\1/pow(10,d)
'z=-1/6 '-0.16667
'z=pi '3.14159
'z=3*4 '12
'z=1 '1
z=10/8 '1.25
print roundto(z,5)
print roundto(1/9, 5) '0.11111
the macro does what I want but is there a better way to achieve the result or a builtin function / macro which I could use?
Roland
Title: Re: Question about round()
Post by: Charles Pegge on March 08, 2015, 12:27:24 PM
Hi Roland,
Try str( 1/9, 5 )
This will express 1/9 to 5 decimal places. Rounding rules are obeyed.
Title: Re: Question about round()
Post by: Mike Lobanovsky on March 08, 2015, 12:29:49 PM
:D
That's a gotcha that used to drive me mad in the past as well. :D
Title: Re: Question about round()
Post by: Charles Pegge on March 08, 2015, 12:36:18 PM
Another similar trick to pad hexadecimals with leading zeros:
print hex(10,4)' 000A
Title: Re: Question about round()
Post by: Mike Lobanovsky on March 08, 2015, 12:38:53 PM
Any analog to standard BASIC Print Using?
Title: Re: Question about round()
Post by: Charles Pegge on March 08, 2015, 01:05:10 PM
Hi Mike,
Not as a core function. Though I have a growing library inc/tableUtil.inc which incorporates C formats, using MSVCRT. It could be extended to accept Basic numeric formats as well.
Do you think the bean-counting professions would ever be interested in OxygenBasic? :)
Title: Re: Question about round()
Post by: Arnold on March 08, 2015, 01:12:27 PM
Sometimes it is a good idea to ask. I did not really notice the meaning of str. I tried: print val(str(pi,5)) * 2 and this works perfectly. In examples\dataprocessing there is a demo format2.o2bas which does a lot of formatting, but this is more than I need in the moment.
Title: Re: Question about round()
Post by: Aurel on March 08, 2015, 01:47:31 PM
Quote
Try str( 1/9, 5 ) This will express 1/9 to 5 decimal places.
grrr i lso have problem with this....well so many things are not documented ::)
Title: Re: Question about round()
Post by: Mike Lobanovsky on March 08, 2015, 02:03:26 PM
Do you think the bean-counting professions would ever be interested in OxygenBasic? :)
Not necessarily. But tell you what, Mr Pegge: without that important feature your OxygenBasic will not toddle for a BASIC at BP.org. The learning curve would be too prohibitive for a Beginner. Against the 8 sacred principles of BASIC. They say.