Restricting number of decimal places

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

Restricting number of decimal places

Post by hinckleyj » Sat 09 Jun 2018, 08:58

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: 7
Joined: Sat 09 Jun 2018, 09:47

Re: Restricting number of decimal places

Post by hellomike » Sat 09 Jun 2018, 09:51

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: 11
Joined: Sat 02 Jun 2018, 08:02

Re: Restricting number of decimal places

Post by hinckleyj » Sat 09 Jun 2018, 10:06

Thanks Mike for your quick reply.
However, @% does not find anything in a full search.

User avatar
hellomike
Posts: 7
Joined: Sat 09 Jun 2018, 09:47

Re: Restricting number of decimal places

Post by hellomike » Sat 09 Jun 2018, 10:28

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: 11
Joined: Sat 02 Jun 2018, 08:02

Re: Restricting number of decimal places

Post by hinckleyj » Sat 09 Jun 2018, 11:00

Perfect!

Thank you Mike. Yes that's clear.

Cheers :D

Post Reply