Increment a numerical string: Difference between revisions
→{{header|AppleScript}}: →AppleScriptObjC: Enhancements and fixes. Vanilla AppleScript solution in preamble.
(→{{header|AppleScript}}: →AppleScriptObjC: Enhancements and fixes. Vanilla AppleScript solution in preamble.) |
|||
Line 515:
----
===AppleScriptObjC===
The task description's delightfully
<syntaxhighlight lang="applescript">
▲The task description's delightfully unspecific about what's meant by "increment" ("add 1" as in C-related languages or "add a certain amount" as in English?) and what's meant by "numerical string" with regard to format, number type, locale, and base. This attempts to cover most bases — except for base itself, which is strictly decimal here.
The following handles more possibilities, but the number base is still resolutely decimal:
<syntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
-- Using the machine's own locale setting. The numerical text must be compatible with this.
-- Params: Numerical text or NSString, AppleScript or Objective-C number or text equivalent.
on incrementNumericalString:str byAmount:
set localeID to current application's class "NSLocale"'s currentLocale()'s localeIdentifier()
return my incrementNumericalString:str byAmount:
end incrementNumericalString:byAmount:
-- Including
-- Params: As above plus locale ID (text or NSString).
on incrementNumericalString:str byAmount:
set |⌘| to current application
set str to |⌘|'s class "NSString"'s stringWithString:(str)
set locale to |⌘|'s class "NSLocale"'s localeWithLocaleIdentifier:(localeID)
set decSeparator to locale's objectForKey:(|⌘|'s NSLocaleDecimalSeparator)
-- We'll use an NSNumberFormatter to generate NSDecimalNumber objects for the math▼
-- as its string conversion methods are more flexible than NSDecimalNumber's own.▼
▲ -- as its number/string
tell |⌘|'s class "NSNumberFormatter"'s new()
its setGeneratesDecimalNumbers:(true)
its setLocale:(locale)
set
if (
--
its setNumberStyle:(|⌘|'s NSNumberFormatterScientificStyle)
set
its setExponentSymbol:(
else
-- Straight
its setNumberStyle:(|⌘|'s NSNumberFormatterDecimalStyle)
set groupingSeparator to locale's objectForKey:(|⌘|'s NSLocaleGroupingSeparator)
its setUsesGroupingSeparator:(str's containsString:(groupingSeparator))
its setMinimumFractionDigits:(str's containsString:(decSeparator))
end if
--
set
set
set sum to (its numberFromString:(str))'s decimalNumberByAdding:(its numberFromString:(increment))
set output to its stringFromNumber:(sum)
end tell
-- Adjustments for AppleScript norms the NSNumberFormatter may omit from scientific notation output:
if (symbolRange's |length|() > 0) then
-- If no decimal separator in the output mantissa, insert point zero or not to match the input style.
if ((eRange's |length|() > 0) and ((str's containsString:((e as text) & "+")) as boolean)) then ¬▼
▲
▲ options:(|⌘|'s NSRegularExpressionSearch) range:({0, output's |length|()})
set output to output's stringByReplacingOccurrencesOfString:("(?<=^-?\\d)") ¬
withString:((decSeparator as text) & "0") options:(regex) range:({0, output's |length|()})
end if
-- If no sign in an E-notation exponent, insert "+" or not ditto.
if (((output's rangeOfString:("[Ee][+-]") options:(regex))'s |length|() > 0) as boolean) then
else if (((str's rangeOfString:("[Ee][+-]") options:(regex))'s |length|() > 0) as boolean) then
set output to output's stringByReplacingOccurrencesOfString:("(?<=[Ee])") ¬
withString:("+") options:(regex) range:({0, output's |length|()})
end if
end if
return output as text -- Return as AppleScript text.
Line 566 ⟶ 586:
(my incrementNumericalString:"12345" byAmount:1), ¬
(my incrementNumericalString:"999,999,999,999,999" byAmount:5), ¬
(my incrementNumericalString:"-1.
(my incrementNumericalString:"-1,
(my incrementNumericalString:"-1.234 x 10^1" byAmount:"10") ¬
}</syntaxhighlight>
{{output}}
(On a machine configured for "en_GB".)
▲<syntaxhighlight lang="applescript">{"12346", "1,000,000,000,000,004", "8.77", "8,77E+0"}</syntaxhighlight>
<syntaxhighlight lang="applescript">{"12346", "1,000,000,000,000,004", "8.766", "-2,34E+0", "-2.34 x 10^0"}</syntaxhighlight>
=={{header|ARM Assembly}}==
|