Interest in custom numeric formatting?

Hi all, I've recently developed a small lib for custom numeric formatting, which is part of many programming languages. You pass any number and a format string which controls the formatting. Some examples (using the US locale): num_format ( 3332123.3332, "$#,0.00" ) -> "$3,332,123.33" num_format ( 123, "#.00" ) -> "123.00" num_format ( 12, "000" ) -> "012" num_format ( 12.9, "000" ) -> "013" (rounded!) num_format ( -123.1, "#.#;-#.00" ) -> "-123.10" num_format ( -123, "$#;- $0.00;0" ) -> "- $123.00" num_format ( 0, "0.00;-0.00;<Zero>" ) -> "<Zero>" num_format ( 1000000, "#,#,.00" ) -> "1,000.00" Short explanation for the format string: . marks the decimal separator , has two functions: - if the format string contains any "," before, but not immediately before the decimal separator, there will be thousand separators in the output. - if the format string contains any "," immediately before the decimal separator, the number will be divided by the number of "," characters multiplied by 1000. 0 If there is no digit in this position, there will be a 0 in the output string # If there is a digit in this position, it is copied to the output string (except non-significant zeros) ; Section separator: if there is one section in the format string, it applies to all values. If there are two sections, the first applies to positive values and to zeros and the second applies to negative values. If there are three sections, the first applies to positive values, the second to negative values and the third to zeros, except if the second section is empty, in which case the first section applies to all non-negative values. Everything in enclosed by ' or " in the format string will be copied to the output as is. I've been looking for a (C++) solution for this for a long time and as I didn't find anything appropriate, I developed it myself finally. If anybody is interested, I could post source somewhere. Stefan

Check out ::strfmon http://www.opengroup.org/onlinepubs/009695399/functions/strfmon.html EXAMPLES Given a locale for the U.S. and the values 123.45, -123.45, and 3456.781, the following output might be produced. Square brackets ( "[]" ) are used in this example to delimit the output. %n [$123.45] Default formatting [-$123.45] [$3,456.78] %11n [ $123.45] Right align within an 11-character field [ -$123.45] [ $3,456.78] %#5n [ $ 123.45] Aligned columns for values up to 99999 [-$ 123.45] [ $ 3,456.78] %=*#5n [ $***123.45] Specify a fill character [-$***123.45] [ $*3,456.78] %=0#5n [ $000123.45] Fill characters do not use grouping [-$000123.45] even if the fill character is a digit [ $03,456.78] %^#5n [ $ 123.45] Disable the grouping separator [-$ 123.45] [ $ 3456.78] %^#5.0n [ $ 123] Round off to whole units [-$ 123] [ $ 3457] %^#5.4n [ $ 123.4500] Increase the precision [-$ 123.4500] [ $ 3456.7810] %(#5n [$ 123.45] Use an alternative pos/neg style [($ 123.45)] [$ 3,456.78] %!(#5n [ 123.45] Disable the currency symbol [( 123.45)] [ 3,456.78] %-14#5.4n [ $ 123.4500 ] Left-justify the output [-$ 123.4500 ] [ $ 3,456.7810 ] %14#5.4n [ $ 123.4500] Corresponding right-justified output [ -$ 123.4500] [ $ 3,456.7810]

tom brinkman wrote:
Check out ::strfmon
http://www.opengroup.org/onlinepubs/009695399/functions/strfmon.html
[...]
Very nice, _but_ - it's quite restricted to currencies, which is of course an obvious application of numeric formatting, but by far not the only one! - this approach opens another way of numeric formatting and is in no way compatible with the common numeric formatting syntax of other languages/spreadsheets etc. - It's a solution for C, not for C++. However, I think I like the alignment feature; I'm just not sure at the moment if it's the trouble... Stefan

"Stefan Slapeta" wrote:
I've recently developed a small lib for custom numeric formatting, which is part of many programming languages. You pass any number and a format string which controls the formatting.
Question: how much does it overlap with functionality of Boost.Format? /Pavel

Pavel Vozenilek wrote:
Question: how much does it overlap with functionality of Boost.Format?
I would say: not at all; boost.format is a replacement for printf to make it typesafe - there's not much more about numeric formatting than in std C. However, custom numeric formatting could be included into boost.format, though this wouldn't be the usual approach as in other languages. I would go this way and add an optional parameter in boost::lexial_cast which would result in a syntax similar to .NET and Java: string s = lexical_cast<std::string>(1231.23, "#,#.00"); Support for numeric format strings is part of the libraries of many common programming languages (e.g. Java and .NET). Although they differ a little bit over the different implementations, I think there is a common base which I tried to implement. Stefan

Stefan Slapeta wrote:
though this wouldn't be the usual approach as in other languages. I would go this way and add an optional parameter in boost::lexial_cast which would result in a syntax similar to .NET and Java:
string s = lexical_cast<std::string>(1231.23, "#,#.00");
Fine idea. Would it work the other way, too, eg. double d = lexical_cast<double>("1,231.23", "#,#.00"); ? B.

Bronek Kozicki wrote:
Fine idea. Would it work the other way, too, eg.
double d = lexical_cast<double>("1,231.23", "#,#.00");
No, I think that's no good idea. This would be the same as removing everything except the decimal separator from the string and use lexical_cast<double> then. Stefan

If you don't post it to a site I'd like to receive a copy at mailto:malcolm@mjfreelancing.com TIA. Malcolm Smith -----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org]On Behalf Of Stefan Slapeta Sent: Monday, 6 September 2004 21:20 To: boost@lists.boost.org Subject: [boost] Interest in custom numeric formatting? Hi all, I've recently developed a small lib for custom numeric formatting, which is part of many programming languages. You pass any number and a format string which controls the formatting. Some examples (using the US locale): num_format ( 3332123.3332, "$#,0.00" ) -> "$3,332,123.33" num_format ( 123, "#.00" ) -> "123.00" num_format ( 12, "000" ) -> "012" num_format ( 12.9, "000" ) -> "013" (rounded!) num_format ( -123.1, "#.#;-#.00" ) -> "-123.10" num_format ( -123, "$#;- $0.00;0" ) -> "- $123.00" num_format ( 0, "0.00;-0.00;<Zero>" ) -> "<Zero>" num_format ( 1000000, "#,#,.00" ) -> "1,000.00" Short explanation for the format string: . marks the decimal separator , has two functions: - if the format string contains any "," before, but not immediately before the decimal separator, there will be thousand separators in the output. - if the format string contains any "," immediately before the decimal separator, the number will be divided by the number of "," characters multiplied by 1000. 0 If there is no digit in this position, there will be a 0 in the output string # If there is a digit in this position, it is copied to the output string (except non-significant zeros) ; Section separator: if there is one section in the format string, it applies to all values. If there are two sections, the first applies to positive values and to zeros and the second applies to negative values. If there are three sections, the first applies to positive values, the second to negative values and the third to zeros, except if the second section is empty, in which case the first section applies to all non-negative values. Everything in enclosed by ' or " in the format string will be copied to the output as is. I've been looking for a (C++) solution for this for a long time and as I didn't find anything appropriate, I developed it myself finally. If anybody is interested, I could post source somewhere. Stefan _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost --- Incoming mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.752 / Virus Database: 503 - Release Date: 03/09/2004 --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.752 / Virus Database: 503 - Release Date: 03/09/2004
participants (6)
-
Bronek Kozicki
-
Malcolm Smith
-
Pavel Vozenilek
-
Stefan Slapeta
-
Stefan Slapeta
-
tom brinkman