Converting numbers to text and back

Ken Ray kray at sonsothunder.com
Sat Nov 30 02:21:01 EST 2002


> Consider this:
>
> on mouseUp
>    set numberFormat to "0.0000000000000000"
>    put 1/3 into f
>    put empty into field "Report"
>    repeat with theE = -100 to 100
>      put f * 10 ^ theE into x
>      put x & lineFeed after field "Report"
>    end repeat
> end mouseUp
>
> For the first several rows I get zeros.  I really want small numbers.
> For the last few rows I get long numbers.


Well, if you don't want scientific notation, and you have some feel for the
total number of digits you want, you can use the format function this way:
(this is using up to 100 characters before the decimal and 100 after):

on mouseUp
  put 1/3 into f
  put empty into field "Report"
  repeat with theE = -100 to 100
    put f * 10 ^ theE into x
    put format("%100.100f",x) & lineFeed after field "Report"
  end repeat
end mouseUp

If you *do* want scientific notation, just change the "f" to "e" and give it
smaller numbers. For example the following provides for 1 character to the
left of the decimal and 4 to the right with scientific notation:

on mouseUp
  put 1/3 into f
  put empty into field "Report"
  repeat with theE = -100 to 100
    put f * 10 ^ theE into x
    put format("%1.4e",x) & lineFeed after field "Report"
  end repeat
end mouseUp

Hope this helps,

Ken Ray
Sons of Thunder Software
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/




More information about the Use-livecode mailing list