Convert numbers to text
I saw that there is a way to convert numbers to text in Numbers, and I’d like to find out how to do it. Can be used for printing a check from a check register.
Windows, Windows 6
I saw that there is a way to convert numbers to text in Numbers, and I’d like to find out how to do it. Can be used for printing a check from a check register.
Windows, Windows 6
Turns out AppleScript is not that easy to do either, though it's easy to use once it's done.
Test results (after converting to Capitalized at Format > Font > Capitalization):
If the script doesn't "do anything" you may need to check System Settings > Privacy & Security > Accessibility to enable Script Editor.
SG
The script:
use framework "Foundation"
use scripting additions
tell front document of application "Numbers"
tell active sheet
tell (first table whose selection range's class is range)
set outStr to ""
repeat with aCell in cells in (get its selection range)
set theNumber to value of aCell
set theInteger to theNumber div 1
set theWords to my convertNumberToWords(theInteger)
set theFraction to my convertDecimalToFraction(theNumber)
set outStr to outStr & theWords & " and " & theFraction & return
end repeat
end tell
end tell
end tell
set the clipboard to outStr
on convertNumberToWords(aNumber)
tell current application's NSNumberFormatter to ¬
set resultingText to localizedStringFromNumber_numberStyle_(aNumber, current application's NSNumberFormatterSpellOutStyle)
return (resultingText as string)
end convertNumberToWords
on convertDecimalToFraction(aNumber)
set integerPart to (aNumber div 1)
set fractionPart to (aNumber - integerPart) * 100
if fractionPart is 0 then
return "00/100"
else if fractionPart mod 10 is 0 then
set fractionPart to fractionPart div 10
return fractionPart & "/10"
else
return fractionPart div 1 & "/100" as text
end if
end convertDecimalToFraction
Turns out AppleScript is not that easy to do either, though it's easy to use once it's done.
Test results (after converting to Capitalized at Format > Font > Capitalization):
If the script doesn't "do anything" you may need to check System Settings > Privacy & Security > Accessibility to enable Script Editor.
SG
The script:
use framework "Foundation"
use scripting additions
tell front document of application "Numbers"
tell active sheet
tell (first table whose selection range's class is range)
set outStr to ""
repeat with aCell in cells in (get its selection range)
set theNumber to value of aCell
set theInteger to theNumber div 1
set theWords to my convertNumberToWords(theInteger)
set theFraction to my convertDecimalToFraction(theNumber)
set outStr to outStr & theWords & " and " & theFraction & return
end repeat
end tell
end tell
end tell
set the clipboard to outStr
on convertNumberToWords(aNumber)
tell current application's NSNumberFormatter to ¬
set resultingText to localizedStringFromNumber_numberStyle_(aNumber, current application's NSNumberFormatterSpellOutStyle)
return (resultingText as string)
end convertNumberToWords
on convertDecimalToFraction(aNumber)
set integerPart to (aNumber div 1)
set fractionPart to (aNumber - integerPart) * 100
if fractionPart is 0 then
return "00/100"
else if fractionPart mod 10 is 0 then
set fractionPart to fractionPart div 10
return fractionPart & "/10"
else
return fractionPart div 1 & "/100" as text
end if
end convertDecimalToFraction
There may be an AppleScript way to do it. I don't recall, though. The formulaic way gets pretty complicated but here goes...
Below is a spreadsheet from 2 years ago that does it in one cell with the help of an auxiliary table. It will do numbers up to 999,999,999.99 (positive and negative, but ignores zero).
Formula in B2 is really simple 🙂. I put it in a code block so it would not get truncated.
TRIM(IF(A<0,"Minus ","")&INDEX(Table 2::$A,TRUNC(ABS(A),−6)×10^−6+1)&" "&INDEX(Table 2::$B,TRUNC(ABS(A),−6)×10^−6+1)&" "&""&INDEX(Table 2::$A,MOD(TRUNC(ABS(A),−3),10^6)×10^−3+1)&" "&INDEX(Table 2::$C,MOD(TRUNC(ABS(A),−3),10^6)×10^−3+1)&" "&" "&INDEX(Table 2::$A,MOD(TRUNC(ABS(A),0),10^3)+1)&""&" "&IF(ABS(A)≥1," and ","")&IF(ABS(A)≠0,RIGHT("00"&MOD(ROUND(ABS(A),2),1)×10^+2,2)&"/100",""))
Copy paste it from this post to cell B2.
Column A of the auxiliary table is every number from one to Nine Hundred Ninety Nine. Columns B and C are as you see them, all the way to the end of the table. I'm trying to recall how I made column A. I didn't type it all by hand, that's for sure. I may have used a different number-to-text method to create the table. Assuming that's what I did, below is the other method. It is only good to 999.99 unless it is expanded further.
Formulas in the upper table are:
C2 =TRUNC(B2,−2)
D2 =TRUNC(B2,0)−C2
E2 =TRUNC(D2,−1)
F2 =D2−E2
G2 =ROUND((B2−C2−D2)×100,0)
H2 =
IF(C2>0,LOOKUP(C2,Table 2::A,Table 2::B)&" ","")&IF(E2>19,LOOKUP(E2,Table 2::A,Table 2::B)&" "&LOOKUP(F2,Table 2::A,Table 2::B),LOOKUP(D2,Table 2::A,Table 2::B))&IF(SUM(C2,D2,F2)>0," and ","")&G2&"/100"
To create text without the "and..." at the end, use this formula for H2 instead of the one above
IF(C2>0,LOOKUP(C2,Table 2::A,Table 2::B)&" ","")&IF(E2>19,LOOKUP(E2,Table 2::A,Table 2::B)&" "&LOOKUP(F2,Table 2::A,Table 2::B),LOOKUP(D2,Table 2::A,Table 2::B))
Do all numbers from 1 to 999 (don't go further) to turn them into text,Select them all, Copy, then Paste Formula Results into column A of the other method.
Convert numbers to text