Restricting number of decimal places

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
hinckleyj
Posts: 25
Joined: Sat 02 Jun 2018, 08:02

Restricting number of decimal places

Post by hinckleyj »

Hi everyone,

When a number is calculated and displayed as, for example, 1.84333333, how do you restrict this to 2 decimal places to just show 1.84?

Thanks.
User avatar
hellomike
Posts: 192
Joined: Sat 09 Jun 2018, 09:47
Location: Amsterdam

Re: Restricting number of decimal places

Post by hellomike »

Hi,

In the help (F1) look for the documentation for '@%'. By using this system variable, you can set how many decimal places to show and much more.

Hope this helps.

Mike
hinckleyj
Posts: 25
Joined: Sat 02 Jun 2018, 08:02

Re: Restricting number of decimal places

Post by hinckleyj »

Thanks Mike for your quick reply.
However, @% does not find anything in a full search.
User avatar
hellomike
Posts: 192
Joined: Sat 09 Jun 2018, 09:47
Location: Amsterdam

Re: Restricting number of decimal places

Post by hellomike »

You mean, nothing in the help?

Here is the link: http://www.bbcbasic.co.uk/bbcwin/manual ... #atpercent

So the syntax is:

@%=&SSNNPPWW

For your need, SS byte is 00, NN byte is 02 (fixed format), PP byte is 02 (decimal places) and WW byte is between 00 and 0A, i.e. total width.
Run the following code to see the differences.

Code: Select all

      V=1.84333333

      PRINT V

      @%=&0002020A : REM Width is 10 (0a) and 2 decimal places
      PRINT V

      @%=&00020306 : REM Width is 6 and 3 decimal places
      PRINT V

      END
Let me know if this makes it more clear.
hinckleyj
Posts: 25
Joined: Sat 02 Jun 2018, 08:02

Re: Restricting number of decimal places

Post by hinckleyj »

Perfect!

Thank you Mike. Yes that's clear.

Cheers :D
KenDown
Posts: 327
Joined: Wed 04 Apr 2018, 06:36

Re: Restricting number of decimal places

Post by KenDown »

However it is usually wise to store the original value of @% before you change it and then restore it later, even if only as part of your exit routine. Otherwise you can get some funny things happening unexpectedly - a typical one with the old BBC Micro (or was it the Arc?) was to discover that your line numbers started having decimal places! That was quite disconcerting the first time it happened!
hinckleyj
Posts: 25
Joined: Sat 02 Jun 2018, 08:02

Re: Restricting number of decimal places

Post by hinckleyj »

Thanks Ken.

I don't have a BBC Micro and am using Richard's BBC 4 Win software.

Could this still happen?