RE: Basic casting question
RE: Basic casting question
- Subject: RE: Basic casting question
- From: Scott Babcock <email@hidden>
- Date: Mon, 2 Mar 2009 15:06:36 -0800
- Acceptlanguage: en-US
- Thread-topic: Basic casting question
If you're converting numeric strings that always use the period as a decimal separator, the most straightforward way to avoid local-format issues is to use the 'run script' event from Standard Additions:
set myString to "339.700012"
set myFloat to (run script myString)
Just as in normal AppleScript source code, the 'run script' event always recognizes the period as the decimal separator, regardless of locale.
-----Original Message-----
Date: Mon, 2 Mar 2009 20:58:37 +0100
From: KOENIG Yvan <email@hidden>
Subject: Re: Basic casting question
To: AppleScript <email@hidden>
Message-ID: <email@hidden>
Content-Type: text/plain; charset=UTF-8; delsp=yes; format=flowed
Le 2 mars 2009 ?? 19:58:16, CYB a ??crit :
>> Hi everyone,
>>
>> I'm surprisingly stuck by a cast I try to do from a string to a real.
>>
>> I have a function that returns something like "339.700012"
>>
>> I want this result to be casted as a real (set myResult to
>> myResult as
>> real) but I always get the same error :
>>
>> "can't make "339.700012" into type real"
>>
>> Is there something I missed regarding casting in AS ?
>>
>> Thanks.
>>
>> St??phane
>
> I think that the problem is where the coercion is happening
> set r to "339.700012"
> r as real -->> 339.700012
> But if I try
> class of r as real -->> Can???t make text into type real.
> If you try
> class of (r as real) -->> Real
>
> So maybe you just need some parenthesis in some place..
No, the problem is linked to the decimal separator.
When the operating system is set to use the period as decimal separator,
casting a string as real requires that in this string, the decimals
are separated from the INT part by a period.
When the operating system is set to use the comma as decimal separator,
casting a string as real requires that in this string, the decimals
are separated from the INT part by a comma.
If you want to build a string which may be coerced worldwide you must
use:
--[SCRIPT]
set deci to character 2 of (.5 as text)
set theString to "98765" & deci &"4321"
set theNumber to theString as real
--[/SCRIPT]
If you aren't the one building the string, you must normalize it as I
described in my message:
De : email@hidden
Objet : R??p : Basic casting question
Date : 1 mars 2009 19:40:48 HNEC
Yvan KOENIG (from FRANCE lundi 2 mars 2009 20:58:32)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden