• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Simple calculation
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Simple calculation


  • Subject: Re: Simple calculation
  • From: email@hidden (Michael Sullivan)
  • Date: Fri, 25 Jan 2002 13:44:25 -0500
  • Organization: Business Card Express

Rob Stott writes:

> Heeeelp!

> Part of my script involves multiplying two figures using the simple lines;

> set theCost to "#" & (327.75 * 50)

> ...I get the result 1.63875E+4 which is all well and good but the script
> forwards the figure to our accountant who is foxed by figures that aren't in
> the format #16387.50

> Is there a way to do this? (Short of getting a smarter accountant...)

Of course. there is always a way.

Behold a fixed string handler.

Probably one of the standard osaxen does this, and I didn't check
applemods either. This rather inelegant hack will work though, and
probably inspire one of the listas to improve it or post their better
solution:

-- begin script
-- all script lines begin with two spaces or a tab indent.
-- Anything starting in the first column represents a line wrap.
-- Nasty, brutish and not so short hack by Michael E. Sullivan, 01/2002

set theCost to "#" & FloatToString(327.75 * 50, 2)

on FloatToString(a, b)
-- a is the number to convert. b is the precision to use

set intA to (round a rounding down) as integer
set decA to FixPrecisionStr(a - intA, b)
set stringA to (intA as string) & (decA as string)
return stringA

end FloatToString

on FixPrecisionStr(a, b)
--a is the number to convert (not in scientific notation), b is the
precision

set {oldTIDs, text item delimiters} to {text item delimiters, "."}
set aList to text items of (a as string)
set decList to characters of item -1 of aList
if (count of decList) < b then
repeat with i from b - (count of decList) to b
set decList to decList & "0"
end repeat

else if (count of decList) < b then
if (item (b + 1) of decList) as integer > 4 then
set theDigit to (item b of decList) as integer
set theDigit to theDigit + 1
set item b of decList to (theDigit as string)
end if
end if

set newDecList to items 1 thru b of decList
set text item delimiters to ""
set theDecimal to newDecList as string
set text item delimiters to "."
set item -1 of aList to theDecimal
set theNum to aList as string
set text item delimiters to oldTIDs
return theNum

end FixPrecisionStr

-- end script


Michael

--
Michael Sullivan
Business Card Express of CT Thermographers to the Trade
Cheshire, CT email@hidden


References: 
 >Simple calculation (From: Rob Stott <email@hidden>)

  • Prev by Date: Re: AS Studio - Variable Troubles - AAAARGHH
  • Next by Date: RE: Weird try block syntax
  • Previous by thread: Re: Simple calculation
  • Next by thread: Re: Simple calculation
  • Index(es):
    • Date
    • Thread