text
stringlengths
0
15.7k
source
stringlengths
6
112
else if formatRecord's roundingBehavior is (rounding halves away from zero) then
lns3.scpt
asocFormatter's setRoundingMode:(current application's NSNumberFormatterRoundHalfUp)
lns3.scpt
else if formatRecord's roundingBehavior is (rounding toward zero) then
lns3.scpt
asocFormatter's setRoundingMode:(current application's NSNumberFormatterRoundDown)
lns3.scpt
else if formatRecord's roundingBehavior is (rounding away from zero) then
lns3.scpt
asocFormatter's setRoundingMode:(current application's NSNumberFormatterRoundUp)
lns3.scpt
else if formatRecord's roundingBehavior is (rounding up) then
lns3.scpt
asocFormatter's setRoundingMode:(current application's NSNumberFormatterRoundCeiling)
lns3.scpt
else if formatRecord's roundingBehavior is (rounding down) then
lns3.scpt
asocFormatter's setRoundingMode:(current application's NSNumberFormatterRoundFloor)
lns3.scpt
error "The ‘number format definition’ record’s ‘roundingBehavior’ is not an allowed constant." number -1703
lns3.scpt
_setBasicFormat(asocFormatter, formatStyle, theNumber)
lns3.scpt
error "Not a ‘number format definition’ record or an allowed constant."
lns3.scpt
on error eText number -1703
lns3.scpt
_support's throwInvalidParameter(formatStyle, "using", {record, constant}, eText)
lns3.scpt
asocFormatter's setLocale:(_support's asNSLocaleParameter(localeCode, "for locale"))
lns3.scpt
return asocFormatter
lns3.scpt
end _makeNumberFormatter
lns3.scpt
to _setBasicFormat(asocFormatter, formatName, theNumber)
lns3.scpt
if formatName is canonical number format then
lns3.scpt
if theNumber is missing value then -- parsing always recognizes scientific notation by default
lns3.scpt
asocFormatter's setNumberStyle:(current application's NSNumberFormatterScientificStyle)
lns3.scpt
else -- formatting switches between notations depending on number's type and value
lns3.scpt
if theNumber's class is integer then -- format integer
lns3.scpt
asocFormatter's setNumberStyle:(current application's NSNumberFormatterNoStyle)
lns3.scpt
else if (-_maxDecimalRange < theNumber and theNumber < -_minDecimalRange) ¬
lns3.scpt
or (_minDecimalRange < theNumber and theNumber < _maxDecimalRange) ¬
lns3.scpt
or theNumber = 0 then -- format real as decimal
lns3.scpt
asocFormatter's setNumberStyle:(current application's NSNumberFormatterDecimalStyle)
lns3.scpt
asocFormatter's setMinimumFractionDigits:1
lns3.scpt
asocFormatter's setMaximumFractionDigits:999 -- kludge; see note below
lns3.scpt
else -- format real as scientific
lns3.scpt
asocFormatter's setFormat:"#.0#E+0" -- for consistency with AS, include '+' in positive exponents (oddly, there doesn't appear to be a property on formatter for setting this)
lns3.scpt
asocFormatter's setMaximumFractionDigits:999
lns3.scpt
else if formatName is scientific format then -- uses exponent notation
lns3.scpt
else if formatName is integer format then -- uses plain integer notation (caution: do not use for reals unless rounding to whole number is intended)
lns3.scpt
else if formatName is decimal format then -- uses thousands separators by default, no exponent
lns3.scpt
else if formatName is currency format then -- adds currency symbol
lns3.scpt
asocFormatter's setNumberStyle:(current application's NSNumberFormatterCurrencyStyle)
lns3.scpt
else if formatName is percent format then -- multiplies by 100 and appends '%'
lns3.scpt
asocFormatter's setNumberStyle:(current application's NSNumberFormatterPercentStyle)
lns3.scpt
else if formatName is word format then -- uses words
lns3.scpt
asocFormatter's setNumberStyle:(current application's NSNumberFormatterSpellOutStyle)
lns3.scpt
else if (count {formatName} each text) ≠ 0 then
lns3.scpt
asocFormatter's setFormat:formatName
lns3.scpt
error "Invalid ‘basic format’ property: not an allowed constant." number -1703 from formatName to constant
lns3.scpt
end _setBasicFormat
lns3.scpt
to _nameForFormat(formatStyle) -- used for error reporting; formatStyle is either constant or text
lns3.scpt
if formatStyle is integer format then
lns3.scpt
return "integer"
lns3.scpt
else if formatStyle is decimal format then
lns3.scpt
return "decimal"
lns3.scpt
else if formatStyle is currency format then
lns3.scpt
return "currency"
lns3.scpt
else if formatStyle is percent format then
lns3.scpt
return "percent"
lns3.scpt
else if formatStyle is scientific format then
lns3.scpt
return "scientific"
lns3.scpt
else if formatStyle is word format then
lns3.scpt
return "word"
lns3.scpt
return "“" & formatStyle & "”"
lns3.scpt
end _nameForFormat
lns3.scpt
to format number theNumber using formatStyle : (canonical number format) for locale localeCode : ("none")
lns3.scpt
if (count {theNumber} each number) = 0 then _support's throwInvalidParameterType(theNumber, "", number, "number") -- only accept integer or real types (i.e. allowing a text parameter to be coerced to number would defeat the purpose of these handlers, which is to avoid unintended localization behavior)
lns3.scpt
set asocFormatter to _makeNumberFormatter(formatStyle, localeCode, theNumber)
lns3.scpt
set asocString to asocFormatter's stringFromNumber:theNumber
lns3.scpt
if asocString is missing value then error "Invalid number (conversion failed)." number -1703 from theNumber -- shouldn't fail, but -stringFromNumber:'s return type isn't declared as non-nullable so check to be sure
lns3.scpt
_error("format number", eText, eNumber, eFrom, eTo)
lns3.scpt
end format number
lns3.scpt
to parse number theText using formatStyle : (canonical number format) for locale localeCode : ("none")
lns3.scpt
if (count {theText} each text) = 0 then _support's throwInvalidParameterType(theText, "", text, "text") -- only accept text, for same reason as above
lns3.scpt
set asocFormatter to _makeNumberFormatter(formatStyle, localeCode, missing value)
lns3.scpt
set asocNumber to asocFormatter's numberFromString:theText
lns3.scpt
if asocNumber is missing value then
lns3.scpt
set localeIdentifier to asocFormatter's locale()'s localeIdentifier() as text
lns3.scpt
if localeIdentifier's length = 0 then -- empty string = system locale
lns3.scpt
set localeIdentifier to "no"
lns3.scpt
set localeIdentifier to "the “" & localeIdentifier & "”"
lns3.scpt
error ("Invalid text (expected numerical text in " & _nameForFormat(formatStyle) & " format for " & localeIdentifier & " locale).") number -1703 from theText
lns3.scpt
return asocNumber as any
lns3.scpt
_error("parse number", eText, eNumber, eFrom, eTo)
lns3.scpt
end parse number
lns3.scpt
to format hex theNumber width chunkSize : (0) prefix hasPrefix : (false)
lns3.scpt
set chunkSize to _support's asIntegerParameter(chunkSize, "width")
lns3.scpt
set hasPrefix to _support's asBooleanParameter(hasPrefix, "prefix") -- (users shouldn't concatenate their own prefix as that would result in negative numbers appearing as "0x-N…" instead of "-0xN…")
lns3.scpt
if (count {theNumber} each list) = 0 then -- format single number
lns3.scpt
set theNumber to _support's asWholeNumberParameter(theNumber, "") -- numbers greater than 2^30 (max integer size) are okay as long as they're non-fractional
lns3.scpt
if chunkSize < 0 or chunkSize > 1024 then _support's throwInvalidParameter(chunkSize, "width", integer, "Must be an integer between 0 and 1024.")
lns3.scpt
set hexText to ""
lns3.scpt
if theNumber < 0 then
lns3.scpt
if chunkSize > 0 and (theNumber < -(16 ^ chunkSize)) then _support's throwInvalidParameter(theNumber, "", integer, "Number is too large to represent as " & chunkSize & "-digit hexadecimal text (not between " & -(16 ^ chunkSize) div 1 & " and " & (16 ^ chunkSize - 1) div 1 & ").")
lns3.scpt
set hexPrefix to "-"
lns3.scpt
set theNumber to -theNumber
lns3.scpt
set hexPrefix to ""
lns3.scpt
if chunkSize > 0 and (theNumber > 16 ^ chunkSize - 1) then _support's throwInvalidParameter(theNumber, "", integer, "Number is too large to represent as " & chunkSize & "-digit hexadecimal text (not between " & -(16 ^ chunkSize) div 1 & " and " & (16 ^ chunkSize - 1) div 1 & ").")
lns3.scpt
if hasPrefix then set hexPrefix to hexPrefix & "0x"
lns3.scpt
repeat while theNumber > 0
lns3.scpt
set hexText to (item (theNumber mod 16 + 1) of "0123456789ABCDEF") & hexText
lns3.scpt
set theNumber to theNumber div 16
lns3.scpt
repeat while length of hexText < chunkSize
lns3.scpt