string-formatting

Advanced formatting rules for StringBuilder.AppendFormat

蹲街弑〆低调 提交于 2019-12-03 20:21:47
I've seen on this site a StringBuilder code sample illustrating AppendFormat usage: using System; using System.Text; class Program { static int[] _v = new int[] { 1, 4, 6 }; static void Main() { StringBuilder b = new StringBuilder(); foreach (int v in _v) { b.AppendFormat("int: {0:0.0}{1}", v, Environment.NewLine); } Console.WriteLine(b.ToString()); } } === Output of the program === int: 1.0 int: 4.0 int: 6.0 Where can I find documentation on those advanced rules for string formatting? Composite Formatting Standard Numeric Format Strings Custom Numeric Format Strings Standard Date and Time

Convert yyyy-MM-ddTHH:mm:ss.fffZ to DateTime in JavaScript manually

故事扮演 提交于 2019-12-03 19:58:05
问题 I receive from a Webservice a String with a date in this format: yyyy-MM-ddTHH:mm:ss.fffZ I need to convert that String with JavaScript to a normal DateTime but without using the new Date('yyyy-MM-ddTHH:mm:ss.fffZ') because I'm using an old version of JavaScript that not support that conversion. I can split that string and get the: Year Month Days Time but how to manipulate the time zone "fffZ" Any suggestions? 回答1: Here's a one liner from John Resig: var date = new Date((time || "").replace(

Python, print all floats to 2 decimal places in output

戏子无情 提交于 2019-12-03 18:26:38
问题 I need to output 4 different floats to two decimal places. This is what I have: print '%.2f' % var1,'kg =','%.2f' % var2,'lb =','%.2f' % var3,'gal =','%.2f' % var4,'l' Which is very unclean, and looks bad. Is there a way to make any float in that out put '%.2f'? Note: Using Python 2.6. 回答1: Well I would atleast clean it up as follows: print "%.2f kg = %.2f lb = %.2f gal = %.2f l" % (var1, var2, var3, var4) 回答2: Format String Syntax. https://docs.python.org/3/library/string.html#formatstrings

casting NSInteger into NSString

房东的猫 提交于 2019-12-03 16:29:44
问题 I'm trying to make one string out of several different parts. This below is what i have right now. I don't get any errors in my code but as soon as i run this and do the event that triggers it i get EXC_BAD_ACCESS . Is there another way of casting this NSInteger into a NSString ? NSString *part1, *part2, *tempString; NSInteger num1; NSInteger num2; part1=@"some"; part2=@"text"; tempString = [NSString stringWithFormat:@"%@%@%@%@", part1, (NSString *)num1, part2, (NSString *)num2]; 回答1: A

C - Format char array like printf

北城以北 提交于 2019-12-03 16:06:50
I want to format a c string like printf does. For example: char string[] = "Your Number:%i"; int number = 33; // String should now be "Your Number:33" Is there any library or a good way I could do this? int main() { char str[] = "Your Number:%d"; char str2[1000]; int number = 33; sprintf(str2,str,number); printf("%s\n",str2); return 0; } Output: ---------- Capture Output ---------- > "c:\windows\system32\cmd.exe" /c c:\temp\temp.exe Your Number:33 > Terminated with exit code 0. sprintf - http://linux.die.net/man/3/sprintf Greenonline For debugging my Arduino sketches, I habitually use this

how to store printf into a variable?

浪子不回头ぞ 提交于 2019-12-03 15:33:32
问题 I want to store a formatted string using something similar to what printf does in C. char *tmp = (char *)sqlite3_column_text(selectstmt, 2); const char *sqlAnswers = printf("select key from answer WHERE key = %s LIMIT 5;", tmp); The latter is an error obviously. 回答1: You can do it with sprintf , but not alone (safely). On a sane system, use snprintf twice, once to find out the size to use and the second time to actually do it. This depends on snprintf returning the number of characters needed

String Format Integer With Commas, No Decimal Places and Nothing for 0 Values

自作多情 提交于 2019-12-03 13:45:36
Okay before I get right into my question does anyone know if there is a tool for building string formats? What I'm thinking is something like a pretty simple user interface where you choose whether you want to display commas, zero values, dollar signs etc and then it spits out the stringformat for you. If there is one I'd love to know about it. If there's not it's the sort of thing people like me would love! My question is. What is the string format for displaying an integer with comma separators for thousands, no decimal places and nothing for zero values. I know the string format for an

How to avoid a Broken Pipe error when printing a large amount of formatted data?

雨燕双飞 提交于 2019-12-03 13:33:38
问题 I am trying to print a list of tuples formatted in my stdout . For this, I use the str.format method. Everything works fine, but when I pipe the output to see the first lines using the head command a IOError occurs. Here is my code: # creating the data data = []$ for i in range(0, 1000): pid = 'pid%d' % i uid = 'uid%d' % i pname = 'pname%d' % i data.append( (pid, uid, pname) ) # find max leghed string for each field pids, uids, pnames = zip(*data) max_pid = len("%s" % max( pids) ) max_uid =

Is there a difference between `%`-format operator and `str.format()` in python regarding unicode and utf-8 encoding?

廉价感情. 提交于 2019-12-03 13:24:06
Assume that n = u"Tübingen" repr(n) # `T\xfcbingen` # Unicode i = 1 # integer The first of the following files throws UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 82: ordinal not in range(128) When I do n.encode('utf8') it works. The second works flawless in both cases. # Python File 1 # #!/usr/bin/env python -B # encoding: utf-8 print '{id}, {name}'.format(id=i, name=n) # Python File 2 # #!/usr/bin/env python -B # encoding: utf-8 print '%i, %s'% (i, n) Since in the documentation it is encouraged to use format() instead of the % format operator, I don't

Format string in python with variable formatting

 ̄綄美尐妖づ 提交于 2019-12-03 13:02:00
How can I use variables to format my variables? cart = {"pinapple": 1, "towel": 4, "lube": 1} column_width = max(len(item) for item in items) for item, qty in cart.items(): print "{:column_width}: {}".format(item, qty) > ValueError: Invalid conversion specification or (...): print "{:"+str(column_width)+"}: {}".format(item, qty) > ValueError: Single '}' encountered in format string What I can do, though, is first construct the formatting string and then format it: (...): formatter = "{:"+str(column_width)+"}: {}" print formatter.format(item, qty) > lube : 1 > towel : 4 > pinapple: 1 Looks