Hi, guys... a stupid question: How can I convert a Number like
1234567 to a String like: 1.234.567 ?. I've been searching all the method... the "Number >> printString" give me the string but without the dots separators. Thanks !. |
Hola MatÃas:
Matias Maretto escribió: > Hi, guys... a stupid question: How can I convert a Number like > 1234567 to a String like: 1.234.567 ?. I've been searching all the > method... the "Number >> printString" give me the string but without > the dots separators. Use a currency converter. E.g. CurrencyToText new currencySymbol: ''; leftToRight: 1234567 "display-it'" Also, you can define what is the decimal separator, and other configurations. Regards, -- Esteban. |
In reply to this post by Matias Maretto
Matias,
> Hi, guys... a stupid question: Not at all. > How can I convert a Number like > 1234567 to a String like: 1.234.567 ?. I've been searching all the > method... the "Number >> printString" give me the string but without > the dots separators. I've a feeling one of the built in string formatting methods (sprintf or format) will do this, but I can't find a reference offhand. It's easy enough to implement though - nb Integers only! Integer>>printStringWithDelimiters | in out | in := self printString reverse readStream. out := String writeStream. [in atEnd] whileFalse: [out nextPutAll: (in nextAvailable: 3). in atEnd ifFalse: [out nextPut: $.]]. ^out contents reverse gives 1 printStringWithDelimiters '1' 12 printStringWithDelimiters '12' 123 printStringWithDelimiters '123' 1234 printStringWithDelimiters '1.234' 12345 printStringWithDelimiters '12.345' 123456 printStringWithDelimiters '123.456' 1234567 printStringWithDelimiters '1.234.567' 12345678 printStringWithDelimiters '12.345.678' 123456789 printStringWithDelimiters '123.456.789' 1234567890 printStringWithDelimiters '1.234.567.890' -- Ian Use the Reply-To address to contact me (limited validity). Mail sent to the From address is ignored. |
In reply to this post by Matias Maretto
Matias Maretto wrote:
> Hi, guys... a stupid question: How can I convert a Number like > 1234567 to a String like: 1.234.567 ?. I've been searching all the > method... the "Number >> printString" give me the string but without > the dots separators. This isn't a stupid question -- it's quite hard to answer properly. You've been given a couple of good simple answers but for a complete solution you need the localisation stuff built into Windows. Unfortunately, Dolphin doesn't (yet?) include a wrapper for the number/currency formatting stuff, so you'd have to do it yourself -- which is not as trivial as one would wish. (MS's fault, not Dolphin's) I have a partial wrapper, if anyone would like a copy. It does localised formatting of numbers OK, but still lacks localised /parsing/ of numerical strings. It also lacks currency handling (which is closely related, but not quite the same). Someday I'll finish it off and perhaps even add some sort of NumberToConverter that uses it. -- chris |
Guys: thanks a lot for the answer... By now I'm going to use the
Ian's code; but Chris would you mind send me a copy of your wrapper?.. my email is: [hidden email]. I 'm starting with the user interface and reports and I fill I'm going to need it. Thanks |
Matias,
> [...] Chris would you mind send me a copy of your wrapper?.. Sent! -- chris |
In reply to this post by Ian Bartholomew-21
> I've a feeling one of the built in string formatting methods (sprintf
> or format) will do this, but I can't find a reference offhand. The method is #formatWith: e.g. 'hello %1!03u!' formatWith: 1 There are many other options using the printf format String that contains the text to be printed. see: http://www.cplusplus.com/ref/cstdio/sprintf.html The format tags follow this prototype: %[flags][width][.precision][modifiers]type where type is the most significant and defines how the value will be printed: type Output Example c Character a d or i Signed decimal integer 392 e Scientific notation (mantise/exponent) using e character 3.9265e2 E Scientific notation (mantise/exponent) using E character 3.9265E2 f Decimal floating point 392.65 g Use shorter %e or %f 392.65 G Use shorter %E or %f 392.65 o Signed octal 610 s String of characters sample u Unsigned decimal integer 7235 x Unsigned hexadecimal integer 7fa X Unsigned hexadecimal integer (capital letters) 7FA p Address pointed by the argument B800:0000 n Nothing printed. The argument must be a pointer to integer where the number of characters written so far will be stored. the other flags, width, .precision and modifiers sub-parameters are optional and follow these specifications: flags meaning - Left align within the given width. (right align is the default). + Forces to preceed the result with a sign (+ or -) if signed type. (by default only - (minus) is printed). blank If the argument is a positive signed value, a blank is inserted before the number. # Used with o, x or X type the value is preceeded with 0, 0x or 0X respectively if non-zero. Used with e, E or f forces the output value to contain a decimal point even if only zeros follow. Used with g or G the result is the same as e or E but trailing zeros are not removed. width meaning number Minimum number of characters to be printed. If the value to be printed is shorter than this number the result is padded with blanks. The value is never truncated even if the result is larger. 0number Same as above but filled with 0s instead of blanks. * The width is not specified in the format string, it is specified by an integer value preceding the argument thas has to be formatted. .precision meaning .number for d, i, o, u, x, X types: precision specifies the minimum number of decimal digits to be printed. If the value to be printed is shorter than this number the result is padded with blanks. The value is never truncated even if the result is larger.(if nothing specified default is 1). for e, E, f types: number of digits to be printed after de decimal point. (if nothing specified default is 6). for g, G types : maximum number of significant numbers to be printed. for s type: maximum number of characters to be printed. (default is to print until first null character is encountered). for c type : (no effect). modifier meaning (affects on how arguments are interpreted by the function) h argument is interpreted as short int (integer types). l argument is interpreted as long int (interger types) or double (floating point types). L argument is interpreted as long double (floating point types). argument(s) Optional parameter(s) that contain the data to be inserted instead of % tags specified in format parameter. There must be the same number of these parameter than the number format tags. |
macta wrote
: > > I've a feeling one of the built in string formatting methods (sprintf > > or format) will do this, but I can't find a reference offhand. > > > The method is #formatWith: That doesn't add the "reader-friendly" separators that Matias was asking for. I don't think there is a way to tell Windows' FormatMessage(), or C's sprintf(), to add separators. let alone do so in a locale-sensitive way. (Which is somewhat surprising really....) -- chris |
Hi Chris,
> macta wrote > : >>> I've a feeling one of the built in string formatting methods >>> (sprintf or format) will do this, but I can't find a reference >>> offhand. >>> >> The method is #formatWith: >> > That doesn't add the "reader-friendly" separators that Matias was > asking for. I don't think there is a way to tell Windows' > FormatMessage(), or C's sprintf(), to add separators. let alone do so > in a locale-sensitive way. (Which is somewhat surprising really....) Yes you are right, I hadn't noticed that - but I notice that when it prints out date formats, it uses the regional settings you define in the control panel (kind of silly numbers don't do that, sigh). Still its a method useful for some things. Tim |
Free forum by Nabble | Edit this page |