Re: Source changes
- Posted by Derek Parnell <ddparnell at bigp?nd.com> Jul 02, 2007
- 662 views
CChris wrote: > 1/ some people, perhaps many of them, are looking for a function that converts > a string into a numerical value and hardly anything else. Derek Parnell hinted > that he would submit a tidied up version of w32to_number(). > 2/ If I understand well what has been said, many people will switch to that > function and leave value() alone anyway. This will certainly decrease the > amount > of possible groaning, as w32to_number(), or whatever it will be eventually > named, > will be "simpler" (= return less data? not sure, but oh well). I've started the process. I'm packaging this function plus a few others into a new library, whose purpose will be to contain text conversion routines. Thus a new library called "convtext.e" will be added to the Euphoria include folder. It will contain a set of related functions that convert text strings into numbers and visa versa. -- Proposed Routines -- This set will attempt to convert the text to the requested format, but if they are unable to they do not fail, instead they return zero. toInteger(sequence TextIn) -- converts the text to an integer. toNumber(sequence TextIn) -- converts the text to an integer or atom as -- appropriate. toDigits(sequence TextIn) -- converts the text to an integer or atom as -- appropriate, but disregards all non-digits -- except the first decimal-separator. toBase(sequence TextIn, integer Base) -- converts the text to an -- integer or atom as appropriate, -- but disregards all non-digits as -- defined by the 'number-base' -- supplied. This set will attempt to convert the text to the requested format, but if they are unable to they do not fail, instead they return a sequence containing the position in the input where conversion stopped, and the result of the conversion as at that point. CInt(sequence TextIn) -- converts the text to an integer. CNum(sequence TextIn) -- converts the text to an integer or atom as -- appropriate. This set will test the text for conversion but will not actually convert it, instead they return a true/false value. isInteger(sequence TextIn) -- checks that the text could be converted -- to an integer isNumber(sequence TextIn) -- checks that the text could be converted -- to an integer or atom Those routines will allow for decimal-separators, number-grouping, leading sign (+/-) or trailing sign, currency symbol, scientific notation, number-bases from 2 to 94, parenthetical negatives, and scaling symbols. The user will be able to set the values for decimal-separator, number-groups, currency symbols, and scaling symbols. By default they will be set to ".", ",_ ", "$¢£¤¥₣₤₧₪₫€", and "%‰" (unicode only). This next function takes a sequence of text strings and returns the same sequence but with as many strings as possible converted to numbers. makeNumbers(sequence RecordData) e.g. makeNumbers({"Derek", "Parnell", "555 7748", "19", "North Road"}) would return {"Derek", "Parnell", 5557748, 19, "North Road"} This set of routines converts a number to a text string, some using locale settings. StringInt( integer Data ) StringNumber( atom Data ) StringFormat( atom Data, sequence Format) DateFormat ( sequence Data, sequence Format) TimeFormat ( atom Data, sequence Format) CurrencyFormat (atom Data) This library can also be used for other text conversions as the need arises. Comments please... -- Derek Parnell Melbourne, Australia Skype name: derek.j.parnell