Number names: Difference between revisions
→{{header|AppleScript}}: Added long-scale options.
No edit summary |
(→{{header|AppleScript}}: Added long-scale options.) |
||
Line 544:
NSNumberFormatter supports several natural languages and by default uses the one set for the user on the host machine. However, its English is limited to US English, so there are no "and"s in the results.
Vanilla AppleScript can be more flexible if you're prepared to write the code. The
Both scripts can display strange results with numbers
<lang applescript>-- Parameters:
-- n: AppleScript integer or real.
-- longScale (optional): boolean. Whether to use long-scale -illions instead of short-scale. Default: false
-- milliards (optional): boolean. Whether to use long-scale -illiards instead of long-scale thousands.
-- ands (optional): boolean. Whether to include "and"s in the result. Default: true.
on numberToEnglish from n given longScale:usingLongScale : false, milliards:usingMilliards : false, ands:usingAnd : true
-- If 'with milliards' is specified, make sure the differently coded 'with longScale' is disabled.
-- Script object containing the word lists and a recursive subhandler.▼
if (usingMilliards) then set (usingLongScale) to false
script o
property scale : 1000
property unitsAndTeens : {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", ¬
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
property tens : {missing value, "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}
property landmarks : {"thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion"}
property illiardLandmarks : {"thousand", "million", "milliard", "billion", "billiard", "trillion", "trilliard", "quadrillion", ¬
"quadrilliard", "quintillion", "quintilliard", "sextillion", "sextilliard", "septillion", "septilliard"}
property collector : {} -- Words collected here.
--
on
-- Recursively work to the "front" of the
if (n
--
set
--
if (
-- In this context, if the group's bottom three digits amount to zero, the appropriate "-illion", if any, must be added here.
set groupValue to groupValue mod 1000
if ((
end if▼
-- Deal with either a short-scale digit group or the bottom three digits of a long-scale one.
if (groupValue > 0) then
nameGroup(groupValue, true, landmarkIndex)
if (landmarkIndex > 0) then set end of my collector to item landmarkIndex of my landmarks
end nameInteger
-- Deal with a value representing a group of up to three digits.
on nameGroup(groupValue, notThousands, landmarkIndex)
-- Firstly the hundreds, if any.
if (groupValue > 99) then
set end of my collector to item (groupValue div 100) of unitsAndTeens
set end of my collector to "hundred"
end if
-- Then the tens and units together, according to whether they require single words, hyphenated words or none.
set tensAndUnits to
if (tensAndUnits > 0) then
-- Insert the word "and" if enabled and appropriate.
Line 579 ⟶ 604:
((collector ends with "hundred") ¬
or (collector ends with "thousand") ¬
or ((
set end of my collector to "and"
if (tensAndUnits < 20) then
Line 592 ⟶ 617:
end if
end if
end nameGroup
▲ if ((hundredsTensAndUnits > 0) and (landmarkNo > 0)) then set end of my collector to item landmarkNo of my landmarks
▲ end parseInt
end script
-- Adjust for a negative if
if (n < 0) then set {end of o's collector, n} to {"minus", -n}
▲ set end of o's collector to "minus"
-- Deal with the
▲ end if
if (n div 1 is 0) then
set end of o's collector to "zero"
else
set o's scale to 1000000
set o's landmarks to rest of o's landmarks
else if (usingMilliards) then
set o's landmarks to o's illiardLandmarks
end if
tell o to nameInteger(n div 1, 0)
end if
-- Deal with any fractional part
if (n mod 1 > 0.0) then
set end of o's collector to "point"
Line 635 ⟶ 663:
end numberToEnglish
numberToEnglish from -3.
--> "minus
numberToEnglish from -3.
--> "minus
numberToEnglish from -3.60287970189634E+12 with longScale
--> "minus three billion six hundred and two thousand eight hundred and seventy-nine million seven hundred and one thousand eight hundred and ninety-six point three four"
numberToEnglish from -3.60287970189634E+12 with milliards
--> "minus three billion six hundred and two milliard eight hundred and seventy-nine million seven hundred and one thousand eight hundred and ninety-six point three four"</lang>
=={{header|Applesoft BASIC}}==
|