• 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
numbers in words
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

numbers in words


  • Subject: numbers in words
  • From: Yvan KOENIG <email@hidden>
  • Date: Sat, 06 Jun 2015 22:55:22 +0200

In the page http://www.macosxautomation.com/applescript/sbrt/sbrt-00.html#1002

I found a Shane Stanley's handler designed to write numbers in words.

It works well but it suffers of an oddity introduced by Apple engineers which decided - what a surprise - that they are the only ones knowing how numbers are written in french.

For them, 123.45 is supposed to be spelled : "cent vingt-trois virgule quatre cinq"
which is wrong.
It must be spelled "cent vingt-trois virgule quarante-cinq" 

As I'm quite sure that getting a corrected code from Apple would require years, I decided to write customised handlers.

Here they are:

use framework "Foundation"

my convertNumberToWords(9876.45)
log result

my convertNumberToWords(98765432)
log result

my convertNumberToLocaleWords:(9876.45) forLocale:"en"
log result

my convertNumberToLocaleWords:(98765432) forLocale:"en"
log result

my convertNumberToLocaleWords:(9876.45) forLocale:"fr"
log result

my convertNumberToLocaleWords:(98765432) forLocale:"fr"
log result

on convertNumberToWords(thisNumber)
# I guess that it's not the best way to grab which locale is used
tell current application's NSNumberFormatter
set midText to localizedStringFromNumber_numberStyle_(1.1, current application's NSNumberFormatterSpellOutStyle) as text
end tell
if midText is "un virgule un" then # we are speaking french
set strNumber to thisNumber as text
if strNumber contains "," then
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {","}
set theParts to text items of strNumber
set AppleScript's text item delimiters to saveTID


tell current application's NSNumberFormatter
set begText to localizedStringFromNumber_numberStyle_((theParts's item 1) as integer, current application's NSNumberFormatterSpellOutStyle)
set endText to localizedStringFromNumber_numberStyle_((theParts's item 2) as integer, current application's NSNumberFormatterSpellOutStyle)
end tell
return (begText as string) & " virgule " & endText
else
tell current application's NSNumberFormatter to set resultingText to localizedStringFromNumber_numberStyle_(thisNumber, current application's NSNumberFormatterSpellOutStyle)
return (resultingText as text)
end if
else
tell current application's NSNumberFormatter to set resultingText to localizedStringFromNumber_numberStyle_(thisNumber, current application's NSNumberFormatterSpellOutStyle)
return (resultingText as text)
end if
end convertNumberToWords

on convertNumberToLocaleWords:thisNumber forLocale:localeString
set theFormatter to current application's NSNumberFormatter's new()
theFormatter's setNumberStyle:(current application's NSNumberFormatterSpellOutStyle)
set theLocale to current application's NSLocale's localeWithLocaleIdentifier:localeString
theFormatter's setLocale:theLocale
if localeString = "fr" then
# Grab the local decimal separator
set deciDelim to item 2 of (0.2 as text) # I'm not sure that we may assume that it's the comma
# Convert the number into text
set strNumber to thisNumber as text
if strNumber contains deciDelim then
# Split the string on the decimal delimiter
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {deciDelim}
set theParts to text items of strNumber
set AppleScript's text item delimiters to saveTID
set begText to theFormatter's stringFromNumber:((theParts's item 1) as integer)
set endText to theFormatter's stringFromNumber:((theParts's item 2) as integer)
return (begText as string) & " virgule " & endText
else
set resultingText to theFormatter's stringFromNumber:thisNumber
return (resultingText as text)
end if
else
set resultingText to theFormatter's stringFromNumber:thisNumber
return (resultingText as text)
end if


end convertNumberToLocaleWords:forLocale:

I know that both handlers apply only to numbers which AppleScript is able to display without using the scientific format.
Maybe one day I will fill this hole…

Alas, I'm not at ease with the help pages of xCode and given that, I found no clean way to get the current localeIdentifier.
I decided to use a workaround which is far from pretty but it does the job.

Of course, if somebody is fair enough to build better handlers, I'm all ears.

Yvan KOENIG (VALLAURIS, France) samedi 6 juin 2015 22:54:31



 _______________________________________________
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

  • Follow-Ups:
    • Re: numbers in words
      • From: Shane Stanley <email@hidden>
  • Prev by Date: Problem with Mavericks/Yosemite library
  • Next by Date: Re: Problem with Mavericks/Yosemite library
  • Previous by thread: Re: Problem with Mavericks/Yosemite library
  • Next by thread: Re: numbers in words
  • Index(es):
    • Date
    • Thread