string-formatting

String.format() throws FormatFlagsConversionMismatchException

爷,独闯天下 提交于 2019-12-01 05:22:22
This code works fine in Java 1.6: public static String padLeft(String s, int n) { if (n <= 0) return s; int noOfSpaces = n * 2; String output; noOfSpaces = s.length() + noOfSpaces; output = String.format("%1$#" + noOfSpaces + "s", s); return output; } But newer versions (and some other VM implementations) throw this Exception : java.util.FormatFlagsConversionMismatchException: Mismatched Convertor =s, Flags= # at java.util.Formatter$Transformer.transformFromString(Formatter.java:1020) at java.util.Formatter$Transformer.transform(Formatter.java:861) at java.util.Formatter.format(Formatter.java

Indenting Paragraph With cout

邮差的信 提交于 2019-12-01 04:52:55
Given a string of unknown length, how can you output it using cout so that the entire string displays as an indented block of text on the console? (so that even if the string wraps to a new line, the second line would have the same level of indentation) Example: cout << "This is a short string that isn't indented." << endl; cout << /* Indenting Magic */ << "This is a very long string that will wrap to the next line because it is a very long string that will wrap to the next line..." << endl; And the desired output: This is a short string that isn't indented. This is a very long string that

Padding number with leading zeros in XSLT 1.0

对着背影说爱祢 提交于 2019-12-01 04:11:36
We have a number in XML that can go up to 3 digits in a large XML file that has to be converted to fixed length text for loading into another system. I need to pad this with leading zeros to a length of 15 in the output (which is fixed length text) Examples: - 1 becomes 000000000000001 - 11 becomes 000000000000011 - 250 becomes 000000000000250 I tried this: <xsl:value-of select="substring(concat('000000000000000', msg:BankAccount/msg:Counter), 12, 15)"/> to get the 15 zeros at the beginning and take the substring but I must have made a mistake with the substring because in the results I get

Dynamic Float Format Specifier in C

蓝咒 提交于 2019-12-01 03:40:20
Is there any way to have a user inputed float format specifier? For example, if I print this. float c = 15.0123 printf("%.2f", c); // outputs: 15.01 How can I assign the number of decimal places to a variable? Like: int n = 3; float c = 15.0123 printf("%.(%i)f", n, c); // outputs: 15.012 The precision can be specified by an argument with the asterisk * . This is called an argument-supplied precision. float c = 15.0123; int m = 2; printf("%.*f", m, c); printf("%.*f", n, c); that will print out c with n places after the decimal. 来源: https://stackoverflow.com/questions/9627075/dynamic-float

How to format a decimal without trailing zeros

[亡魂溺海] 提交于 2019-12-01 03:34:50
I've just learned that a decimal somehow remembers how much trailaing zero's were needed to store a number. With other words: it remembers the size of the fraction. For example: 123M.ToString() ==> resuls in: 123 123.00M.ToString() ==> resuls in: 123.00 123.450M.ToString() ==> resuls in: 123.450 I am looking for a formatting string or another trick to get rid of those "unneeded" trailing zeros, but keeping the significant digits. So: 123M.ToString() ==> resuls in: 123 123.00M.ToString() ==> resuls in: 123 123.450M.ToString() ==> resuls in: 123.45 Removing the zeros at the end of the new string

How to use a dot in Python format strings?

為{幸葍}努か 提交于 2019-12-01 03:31:15
I want to format a string and be able to use the dot operator, so that I can construct template strings containing e.g. {user.name} , {product.price} . I tried this: 'Hello {user.name}'.format( {'user': { 'name': 'Markus' } } ) KeyError: 'user' 'Hello {user.name}'.format( **{'user': { 'name': 'Markus' } } ) AttributeError: 'dict' object has no attribute 'name' Is there a way to do it? Python dict objects are unfortunately not attribute accessible (i.e. with the dot notation) by default. So you can either resign yourself to the uglier brackets notation: 'Hello {user[name]}'.format( **{'user': {

PHP money_format

丶灬走出姿态 提交于 2019-12-01 03:27:48
I'm using on money_format with the first parameter being '%n' to include the dollar sign, and I have the locale set to en_US but it still doesn't include it. Why? From the PHP.net comment : If money_format doesn't seem to be working properly, make sure you are defining a valid locale. For example, on Debian, 'en_US' is not a valid locale - you need 'en_US.UTF-8' or 'en_US.ISO-8559-1'. This was frustrating me for a while. Debian has a list of valid locales at /usr/share/i18n/SUPPORTED; find yours there if it's not working properly. 来源: https://stackoverflow.com/questions/4158517/php-money

Lisp string formatting with named parameters

ⅰ亾dé卋堺 提交于 2019-12-01 02:59:19
Is there a way in Lisp to format a string using named parameters? Perhaps something with association lists like (format t "All for ~(who)a and ~(who)a for all!~%" ((who . "one"))) in order to print "All for one and one for all" . Similar to this python question , or this scala one , or even c++ , but in Lisp. If this functionality isn't in the language, does anyone have any cool functions or macros that could accomplish the same thing? Use CL-INTERPOL . (cl-interpol:enable-interpol-syntax) String interpolation For simple cases, you don't need FORMAT : (lambda (who) #?"All for $(who) and $(who)

Why does using a list as a string formatting parameter, even with no %s identifier, return the original string?

ぃ、小莉子 提交于 2019-12-01 02:59:02
>>> 'string with no string formatting markers' % ['string'] 'string with no string formatting markers' >>> 'string with no string formatting markers' % ('string',) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: not all arguments converted during string formatting I would expect both cases to raise a TypeError , but this is not the case. Why not? The Python documentation on this subject talks about strings, tuples and dictionaries, but says nothing about lists. I'm a bit confused about this behavior. I've been able to duplicate it in Python 2.7 and 3.2.

Can maximum number of characters be defined in C# format strings like in C printf?

不羁岁月 提交于 2019-12-01 02:41:37
Didn't find how to do that. What I found was more or less on the lines of this ( http://blog.stevex.net/string-formatting-in-csharp/ ): There really isn’t any formatting within a string, beyond it’s alignment. Alignment works for any argument being printed in a String.Format call. Sample Generates String.Format(“->{1,10}<-”, “Hello”); // gives "-> Hello<-" (left padded to 10) String.Format(“->{1,-10}<-”, “Hello”); // gives "->Hello <-" (right padded to 10) What you want is not "natively" supported by C# string formatting, as the String.ToString methods of the string object just return the