----- Original Message -----
From: Malcolm Lear <[email protected]>
To: [email protected]
Cc: 
Sent: Monday, 5 March 2012, 11:28
Subject: Re: [Ql-Users] Printing numbers in non exponent form

It was I who required a way to do this. Thanks for this. Very well timed since 
I now need to print unsigned 32 bit integers and unfortunately none of the DEC$ 
functions can do this. Hopefully a few changes to this will provide a solution.


On 05/03/2012 09:59, Michael Bulford wrote:
> Some weeks ago
> someone enquired as to how to print numbers in non exponent form.  I believe 
> it was George Gwilt who suggested
> using CDEC$.  Here is a function that I
> use ...
> 
>   750 DEFine
> FuNction comma$(num)
> 760 LOCal t, fra
> 770  IF num<  0 : RETurn "-"
> &  comma$(ABS(num))
> 780  IF num<  1000 : RETurn num
> 790  IF num/1000>  2^31-2 : RETurn num
> : REMark otherwise INT(num/1000) will fail
> 800  t = INT(num/1000)
> 810  fra = num – t * 1000 : IF
> "e-" INSTR fra : fra = 0
> 820  RETurn comma$((t))&
> ","&  FILL$("0",(fra<100)+(fra<10)+(fra<1 AND
> fra>0))&  fra
> 830 END DEFine comma$
>   This only works
> properly for whole numbers, and if the number cannot be converted then the
> exponent form is returned.
>   PRINT
> comma$(2^31) gives 2,147,483,648
>   PRINT
> comma$(1000.753) gives 1,000.7529998
>   Michael Bulford
> 

I have been having problems with my browser, but have now blocked addons.  
Hopefully, the newlines being inserted into my text will not happen.  The text 
should have read as follows ...

Printing numbers in non exponent form

Some weeks ago someone 
enquired as to how to print numbers in non exponent form.  I believe it 
was George Gwilt who suggested using CDEC$.  Here is a function that I 
use ...

750 DEFine FuNction comma$(num)
760 LOCal t, fra
770  IF num < 0 : RETurn "-" & comma$(ABS(num))
780  IF num < 1000 : RETurn num
790  IF num/1000 > 2^31-2 : RETurn num : REMark otherwise INT(num/1000) will 
fail
800  t = INT(num/1000)
810  fra = num – t * 1000 : IF "e-" INSTR fra : fra = 0
820  RETurn comma$((t)) & "," & FILL$("0",(fra<100)+(fra<10)+(fra<1 AND fra>0)) 
& fra
830 END DEFine comma$

This only works properly for whole numbers, and if the number cannot be 
converted then the exponent form is returned.

PRINT comma$(2^31) gives 2,147,483,648

PRINT comma$(1000.753) gives 1,000.7529998

The CDEC$ function range is +/- up to only 1E9 and is difficult to use as it 
has 3 parameters.  It is however, highly accurate when printing numbers between 
zero and one!

Michael Bulford
_______________________________________________
QL-Users Mailing List
http://www.q-v-d.demon.co.uk/smsqe.htm

Reply via email to