Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Currency Numbers



Title: Re: Currency Numbers
At 6:15 PM +0200 6/19/06, Wayne Melrose wrote:
On Jun 19, 2006, at 4:55 PM, David Bradley wrote:

Hi,

Can you format numbers to 2 decimal points in Applescript. I want to format the numbers in to a currency format

David,

this is a handler that someone posted once before when I asked the same question...

 Sorry whoever that was ... otherwise I would give you the credit for it!


--- used for rounding number
on roundThis(n, numDecimals)
        set x to 10 ^ numDecimals
        (((n * x) + 0.5) div 1) / x
end roundThis

I don't know the author of this version either, but it will handle numbers with exponents if necessary:

round_truncate(1.04583, 3)
--
> "1.046"


on round_truncate(this_number, decimal_places)
    
if decimal_places is 0 then
            
set this_number to this_number + 0.5
           
return number_to_text(this_number div 1)
       
end if
 
       
set the rounding_value to "5"
  
repeat decimal_places times
            
set the rounding_value to "0" & the rounding_value
     
end repeat
     
set the rounding_value to ("." & the rounding_value) as number
 
       
set this_number to this_number + rounding_value

       
set the mod_value to "1"
       
repeat decimal_places - 1 times
       
set the mod_value to "0" & the mod_value
       
end repeat
     
set the mod_value to ("." & the mod_value) as number
   
       
set second_part to (this_number mod 1) div the mod_value
       
if the length of (the second_part as text) is less than the ¬
          
decimal_places then
            
repeat decimal_places - ¬
                       (
the length of (the second_part as text)) times
               
set second_part to ("0" & second_part) as string
               
end repeat
     
end if
 
       
set first_part to this_number div 1
    
set first_part to number_to_text(first_part)
   
set this_number to (first_part & "." & second_part)
    
       
return this_number
end round_truncate

on number_to_text(this_number)
     
set this_number to this_number as text
 
if this_number contains "E+" then
              
set x to the offset of "." in this_number
              
set y to the offset of "+" in this_number
              
set z to the offset of "E" in this_number
              
set the decimal_adjust to characters (y - (length of this_number)) thru ¬
                       -1
of this_number as string as number
          
if x is not 0 then
                     
set the first_part to characters 1 thru (x - 1) of this_number as string
               
else
                   
set the first_part to ""
               
end if
         
set the second_part to characters (x + 1) thru (z - 1) of this_number as string
       
set the converted_number to the first_part
             
repeat with i from 1 to the decimal_adjust
                     
try
                            
set the converted_number to ¬
                                  
the converted_number & character i of the second_part
                  
on error
                               
set the converted_number to the converted_number & "0"
                 
end try
       
end repeat
             
return the converted_number
    
else
           
return this_number
     
end if
end number_to_text
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/applescript-users/email@hidden

This email sent to email@hidden

References: 
 >Currency Numbers (From: David Bradley <email@hidden>)
 >Re: Currency Numbers (From: Wayne Melrose <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.