RosettaCodeData/Task/Commatizing-numbers/00-TASK.txt

81 lines
4.7 KiB
Plaintext

''Commatizing''   numbers (as used here, is a handy expedient made-up word) is the act of adding commas to a number (or string), or to the numeric part of a larger string.
;Task:
Write a function that takes a string as an argument with optional arguments or parameters (the format of parameters/options is left to the programmer) that in general, adds commas (or some
other characters, including blanks or tabs) to the first numeric part of a string (if it's suitable for commatizing as per the rules below), and returns that newly commatized string.
Some of the commatizing rules (specified below) are arbitrary, but they'll be a part of this task requirements, if only to make the results consistent amongst national preferences and other disciplines.
The number may be part of a larger (non-numeric) string such as:
::::*   «US$1744 millions»       ──or──
::::*   ±25000 motes.
<br>
The string may possibly ''not'' have a number suitable for commatizing, so it should be untouched and ''no error generated''.
If any argument (option) is invalid, nothing is changed and no error ''need be'' generated (quiet execution, no fail execution). &nbsp; Error message generation is optional.
The exponent part of a number is never commatized. &nbsp; The following string isn't suitable for commatizing: &nbsp; 9.7e+12000
Leading zeroes are never commatized. &nbsp; The string &nbsp; 0000000005714.882 &nbsp; after commatization is: &nbsp; 0000000005,714.882
Any &nbsp; ''period'' &nbsp; (<big><b>.</b></big>) &nbsp; in a number is assumed to be a &nbsp; ''decimal point''.
The original string is never changed &nbsp; ''except'' &nbsp; by the addition of commas &nbsp; [or whatever character(s) is/are used for insertion], if at all.
To wit, the following should be preserved:
::* &nbsp; leading signs ('''+''', '''-''') &nbsp; &nbsp; &nbsp; ── even superfluous signs
::* &nbsp; leading/trailing/embedded blanks, tabs, and other whitespace
::* &nbsp; the case (upper/lower) of the exponent indicator, e.g.: &nbsp; 4.8903d-002
<br>
Any exponent character(s) should be supported:
::::::* &nbsp; 1247e12
::::::* &nbsp; 57256.1D-4
::::::* &nbsp; 4444^60
::::::* &nbsp; 7500<b>∙</b>10**35
::::::* &nbsp; 8500x10**35
::::::* &nbsp; 9500↑35
::::::* &nbsp; +55000↑3
::::::* &nbsp; 1000**100
::::::* &nbsp; 2048²
::::::* &nbsp; 4096<sup>32</sup>
::::::* &nbsp; 10000pow(pi)
<br>Numbers may be terminated with any non-digit character, including subscripts and/or superscript: &nbsp; 4142135624<sup>3</sup> &nbsp; or &nbsp; 7320509076<sub>(base 24)</sub>.
The character(s) to be used for the comma can be specified, and may contain blanks, tabs, and other whitespace characters, as well as multiple characters. &nbsp; The default is the comma (<big>''','''</big>) character.
The &nbsp; ''period length'' &nbsp; can be specified &nbsp; (sometimes referred to as "thousands" or "thousands separators"). &nbsp; The &nbsp; ''period length'' &nbsp; can be defined as the length (or number) of the decimal digits between commas. &nbsp; The default period length is &nbsp; <big>3</big>.
::: E.G.: &nbsp; in this example, the &nbsp; ''period length'' &nbsp; is five: &nbsp; 56789,12340,14148
The location of where to start the scanning for the target field (the numeric part) should be able to be specified. &nbsp; The default is &nbsp; <big>1</big>.
The character strings below may be placed in a file (and read) or stored as simple strings within the program.
;Strings to be used as a minimum:
The value of &nbsp; pi &nbsp; (expressed in base 10) &nbsp; should be separated with blanks every &nbsp; '''5''' &nbsp; places past the decimal point,
<br>the Zimbabwe dollar amount should use a decimal point for the "comma" separator:
:* &nbsp; pi=3.14159265358979323846264338327950288419716939937510582097494459231
:* &nbsp; The author has two Z$100000000000000 Zimbabwe notes (100 trillion).
:* &nbsp; "-in Aus$+1411.8millions"
:* &nbsp; ===US$0017440 millions=== (in 2000 dollars)
:* &nbsp; 123.e8000 is pretty big.
:* &nbsp; The land area of the earth is 57268900(29% of the surface) square miles.
:* &nbsp; Ain't no numbers in this here words, nohow, no way, Jose.
:* &nbsp; James was never known as 0000000007
:* &nbsp; Arthur Eddington wrote: I believe there are 15747724136275002577605653961181555468044717914527116709366231425076185631031296 protons in the universe.
:* &nbsp; ␢␢␢$-140000±100 millions.
:* &nbsp; 6/9/1946 was a good year for some.
<br>where the penultimate string has three leading blanks &nbsp; (real blanks are to be used).
;Also see:
* The Wiki entry: &nbsp; [http://en.wikipedia.org/wiki/Eddington_number (sir) Arthur Eddington's number of protons in the universe]. <br><br>