Author Topic: Question about round()  (Read 3639 times)

0 Members and 1 Guest are viewing this topic.

Arnold

  • Guest
Question about round()
« on: March 08, 2015, 11:22:03 AM »
Hi Charles,

for rounding a fractionial decimal number I currently use a macro:

Code: OxygenBasic
  1. macro roundto(x,d) round(x*pow(10,d))\1/pow(10,d)
  2.  
  3. 'z=-1/6     '-0.16667
  4. 'z=pi       '3.14159
  5. 'z=3*4      '12
  6. 'z=1        '1
  7. z=10/8     '1.25
  8.  
  9. print roundto(z,5)
  10. print roundto(1/9, 5) '0.11111
  11.  

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

Charles Pegge

  • Guest
Re: Question about round()
« Reply #1 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.

Mike Lobanovsky

  • Guest
Re: Question about round()
« Reply #2 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

Charles Pegge

  • Guest
Re: Question about round()
« Reply #3 on: March 08, 2015, 12:36:18 PM »

Another similar trick to pad hexadecimals with leading zeros:

print hex(10,4) ' 000A

Mike Lobanovsky

  • Guest
Re: Question about round()
« Reply #4 on: March 08, 2015, 12:38:53 PM »
Any analog to standard BASIC Print Using?

Charles Pegge

  • Guest
Re: Question about round()
« Reply #5 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? :)


Arnold

  • Guest
Re: Question about round()
« Reply #6 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.

Aurel

  • Guest
Re: Question about round()
« Reply #7 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   ::)

Mike Lobanovsky

  • Guest
Re: Question about round()
« Reply #8 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.

LOL