string-formatting

Using an array as argument for string.Format()

醉酒当歌 提交于 2019-12-01 02:33:36
When trying to use an array as an argument for the string.Format() method, I get the following error: FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list. The code is as follows: place = new int[] { 1, 2, 3, 4}; infoText.text = string.Format("Player1: {0} \n Player2: {1} \n Player3: {2} \n Player4: {3}", place); The Array contains four values and the arguments in the String.Format() are also the same. What causes this error? (The infoText.text is just a regular String object) You can convert int array to string array as pass it.

What does the dollar sign ($) do in a printf format string? [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-01 00:58:53
This question already has an answer here: Selecting parameters in String.format() [duplicate] 3 answers I am trying to format in java using the printf statement like in this webpage: Click Here . But I just can't figure out what the purpose of the $ sign is. Can someone please explain this to me? Input: java 100 cpp 65 python 50 Expected Output: ( there should be a space instead of _ ) ================================ java___________100 cpp___________065 python_________050 ================================ My code: public class Solution { public static void main(String[] args) { Scanner sc=new

Format: add trailing spaces to character output to left-justify

这一生的挚爱 提交于 2019-12-01 00:49:18
How do you format a string to have constant width and be left-justified? There is the Aw formatter, where w denotes desired width of character output, but it prepends the spaces if w > len(characters) , instead of appending them. When I try 44 format(A15) print 44, 'Hi Stack Overflow' I get > Hi Stack Overflow< instead of >Hi Stack Overflow < Is there any simple Fortran formatting solution that solves this? As noted in the question, the problem is that when a character expression of length shorter than the output field width the padding spaces appear before the character expression. What we

How to print “justified” text in the console using modern C++

烂漫一生 提交于 2019-12-01 00:42:58
How can I format text "justified" so it aligns to the left and right side for a given width? int main() { printJustified("A long text with many words. " "A long text with many words. " "A long text with many words. " "A long text with many words. " "A long text with many words."); } Expected output: A long text with many words. A long text with many words. A long text with many words. A long text with many words. A simple example how to solve this problem is this: #include <iostream> #include <sstream> #include <list> const int pageWidth = 78; typedef std::list<std::string> WordList; WordList

Dynamic Float Format Specifier in C

别等时光非礼了梦想. 提交于 2019-12-01 00:24:24
问题 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 回答1: 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); 回答2: printf("%.*f", n, c); that will

python: dots in the name of variable in a format string

*爱你&永不变心* 提交于 2019-11-30 23:47:55
问题 Say I've got a dictionary with dots in the name of fields, like {'person.name': 'Joe'} . If I wanted to use this in str.format , is it possible? My first instinct was 'Name: {person.name}'.format(**{'person.name': 'Joe'}) but this would only work if my dict were shaped like {'person':{'name':Joe}} The relevant manual docs section doesn't mention anyway of escaping the dot. (Sidenote: I thought that generally def func(**kw): print(kw) func(**{'a.b': 'Joe'}) would cause an error, but the **

How to format a decimal without trailing zeros

送分小仙女□ 提交于 2019-11-30 23:47:29
问题 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() ==>

Format decimal with commas, preserve trailing zeros

天大地大妈咪最大 提交于 2019-11-30 23:21:39
问题 I'd like to convert a decimal to a string, with commas as thousands seperators, and preserve the same precision the decimal was created with. (Will have 2-5 significant digits) decimal d = 1234.4500M; //I'd like "1,234.4500" var notRight = d.ToString("###,###.#######"); //1,234.45 var alsoNotRight = d.ToString("###,###.00000");; //1,234.45000 var notRightEither = d.ToString("N"); //1,234.45 var notRightEither2 = d.ToString("G"); //1234.45000 Is there no built in way to do this without parsing

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

时间秒杀一切 提交于 2019-11-30 22:45:43
问题 >>> '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

WPF XAML StringFormat: Culture Workaround broken in C# 4.0?

女生的网名这么多〃 提交于 2019-11-30 22:25:01
The work around... FrameworkElement.LanguageProperty.OverrideMetadata( typeof(FrameworkElement), new FrameworkPropertyMetadata( XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag))); ...used to work till now (mentioned here: StringFormat Localization issues in wpf ). Instead till I ported my application from 3.5SP1 to 4.0, it was working. But now in 4.0 it stopped working again. Anybody experiencing this? EDIT: It is now not even working in 3.5SP1. I think this has something to do with the installation of 4.0 as previously this was working. It is not working by either adding