Re: Convert long number to string
Re: Convert long number to string
- Subject: Re: Convert long number to string
- From: Nigel Garvey <email@hidden>
- Date: Mon, 19 Apr 2004 12:37:34 +0100
Gnarlodious wrote on Sat, 17 Apr 2004 09:48:34 -0600:
>
set longNumber to to "1082204521"
>
set exponent to longNumber as number --> 1.082204521E+9
>
set numberString to exponent as string --> "1.082204521E+9"
>
>
I want to convert the exponent to long number before converting it to a
>
string.
Martin's "as inches as string" suggestion is better for this, but just to
plug my own numToStr handler, which is available at:
http://scriptbuilders.net/category.php?id=1701
... or at least it will be when Macscripter.net's servers come back on
line. It's quite large, but does the job effectively and can handle all
the problem numbers of which I know. Unlike Satimage's 'format' command,
it doesn't need to be given the format in advance.
Gnarlodious wrote on Sat, 17 Apr 2004 16:08:29 -0600:
>
set realNumber to exponent as miles as string
>
set realNumber to exponent as centimeters as string
>
set realNumber to exponent as liters as string
>
(for a goof, write "litres" and watch what happens!)
Yeah. You can use either the American or the English spelling, but you're
still stuck with the US gallon as its non-metric equivalent. :-)
>
So I was tempted to say:
>
>
set realNumber to exponent as seconds as string
>
>
... which fails miserably.
'Seconds', of course, isn't one of AppleScript's measurement unit
classes. These are really intended as a convenient way of converting
between different measurement units. However, it's better to use straight
maths if you know the conversion formulae, as measurement unit coercions
take considerably longer and the results aren't always usable:
32 as degrees Fahrenheit as degrees Celsius as string
--> " 0e+0"
... or accurate:
1 as miles as inches
--> inches 6.33599999036808E+4
>
Seems a little rotund since my script calculates
>
seconds of the UNIX epoch:
>
>
set x to (do shell script "date +%s") as number
>
x - x mod 604800 as meters as string -->1081987200 (on 2004/4/17)
For the numbers you're likely to get here, coercing to measurement unit
and then to string should be fine. The measurement unit that requires the
least typing is 'feet'. :-)
Graff wrote on Sat, 17 Apr 2004 22:22:30 -0400:
>
You can do this a bit more easily in one line:
>
>
set realNumber to do shell script "echo `date +%s` - `date +%s` %
>
604800 | bc"
This produces an empty string on my machine.
I know that shell script aficionados aren't normally concerned about
performance or legibility, but for comparison:
set t to getMilliSec
set x to (do shell script "date +%s") as number
set a to x - x mod 604800 as meters as string
set t1 to getMilliSec
set b to do shell script "echo 'date +%s' - 'date +%s' % 604800 | bc"
set t2 to getMilliSec
set c to ((current date) - (time to GMT) - (date "Thursday 1 January
1970 00:00:00")) div weeks * weeks as feet as string
set t3 to getMilliSec
{shell1:{a, t1 - t}, shell2:{b, t2 - t1}, vanilla:{c, t3 - t2}
--> {shell1:{"1081987200", 21.0}, shell2:{"", 31.0},
vanilla:{"1081987200", 1.0}}
(Timed on a G3 400 PowerBook running 10.2.8.)
NG
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.