In Sinclair BASIC how do i display a number to two decimal points?
eg' 13.52
decimal point
- ParadigmShifter
- Manic Miner
- Posts: 670
- Joined: Sat Sep 09, 2023 4:55 am
Re: decimal point
PRINT INT(x * 100)/100
or
PRINT INT(x * 100 + 0.5)/100
if you want rounding
Here's a function version which takes the number of significant digits s as second parameter
DEF FN r(x,s)=INT(x*(10^s)+0.5)/(10^s)
It's slow and I can't remember how many significant digits the 5 byte floats have in Basic are but I wouldn't use more than 5 and expect accurate results I think. Can probably speed it up using a subroutine but you couldn't pass local parameters to that (but you would only evaluate 10^s once then to speed it up)
If you want fixed width numbers so 1.234 and 12.34 say (both same width, 5 including the decimal point), you would use STR$ to convert a number into a string then do stuff with that based on the length (probably have to turn it into scientific notation if it was too wide?) Or you could use logarithms which would be a lot slower
or
PRINT INT(x * 100 + 0.5)/100
if you want rounding
Here's a function version which takes the number of significant digits s as second parameter
DEF FN r(x,s)=INT(x*(10^s)+0.5)/(10^s)
It's slow and I can't remember how many significant digits the 5 byte floats have in Basic are but I wouldn't use more than 5 and expect accurate results I think. Can probably speed it up using a subroutine but you couldn't pass local parameters to that (but you would only evaluate 10^s once then to speed it up)
If you want fixed width numbers so 1.234 and 12.34 say (both same width, 5 including the decimal point), you would use STR$ to convert a number into a string then do stuff with that based on the length (probably have to turn it into scientific notation if it was too wide?) Or you could use logarithms which would be a lot slower
- 1024MAK
- Bugaboo
- Posts: 3123
- Joined: Wed Nov 15, 2017 2:52 pm
- Location: Sunny Somerset in the U.K. in Europe
Re: decimal point
Convert it to a string and then use the string slicing to chop off the bit you don’t want.
Mark
Mark
Standby alert
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb
Looking forward to summer later in the year.
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb
Looking forward to summer later in the year.
- ParadigmShifter
- Manic Miner
- Posts: 670
- Joined: Sat Sep 09, 2023 4:55 am
Re: decimal point
Yeah I suggested something like that in my edit (use STR$).
You can't round easily doing that and if you have to search for the decimal point it will probably be slower? Both methods have issues if the number would be displayed in scientific notation. But you can pad numbers with no decimal point so they show zeroes that way too (so 1 -> 1.00). Fixed width strings have a length issue with negative numbers too (probably want to pad positive numbers with a leading space then) so you would get output like this
If you use one of the modern basic compilers on the PC that produce compiled code that will run on the spectrum they will most likely have a function which will do much better string formatting.
You can't round easily doing that and if you have to search for the decimal point it will probably be slower? Both methods have issues if the number would be displayed in scientific notation. But you can pad numbers with no decimal point so they show zeroes that way too (so 1 -> 1.00). Fixed width strings have a length issue with negative numbers too (probably want to pad positive numbers with a leading space then) so you would get output like this
Code: Select all
12.34
-45.67
- ParadigmShifter
- Manic Miner
- Posts: 670
- Joined: Sat Sep 09, 2023 4:55 am
Re: decimal point
Another thing you can do if the numbers represent currency (or something which should never be rounded) is store the value * 100 and do this
Or a version which builds a string in a$ and has currency symbol in c$, pads non negative amounts with a leading space, and an example of how to print something right justified (you have to watch that the width is actually less than or equal to the specified width though). Also probably want to pad with spaces at the beginning so it would also work if the money was counting down instead.
Or a version which builds a string in a$ and has currency symbol in c$, pads non negative amounts with a leading space, and an example of how to print something right justified (you have to watch that the width is actually less than or equal to the specified width though). Also probably want to pad with spaces at the beginning so it would also work if the money was counting down instead.