string-formatting

Python string formatting when string contains “%s” without escaping

假装没事ソ 提交于 2019-11-30 08:20:14
When formatting a string, my string may contain a modulo "%" that I do not wish to have converted. I can escape the string and change each "%" to "%%" as a workaround. e.g., 'Day old bread, 50%% sale %s' % 'today!' output: 'Day old bread, 50% sale today' But are there any alternatives to escaping? I was hoping that using a dict would make it so Python would ignore any non-keyword conversions. e.g., 'Day old bread, 50% sale %(when)s' % {'when': 'today'} but Python still sees the first modulo % and gives a: TypeError: not enough arguments for format string You could (and should) use the new

string.Format fails at runtime with array of integers

谁说胖子不能爱 提交于 2019-11-30 08:05:42
Consider string.Format() whose parameters are a string and, among others in the overload list, an object[] or many objects. This statement succeeds: string foo = string.Format("{0} {1}", 5, 6); as does this: object[] myObjs = new object[] {8,9}; string baz = string.Format("{0} and {1}", myObjs; as does an array of strings: string[] myStrings = new string[] {"abc", "xyz"}; string baz = string.Format("{0} {1}", myStrings); It seems that the integers, when specified individually, can be boxed or coerced to type object , which in turn is coerced to a string. This statement fails at runtime . int[]

Show decimal of a double only when needed

主宰稳场 提交于 2019-11-30 08:01:35
I got this problem with double (decimals). When a double = 1.234567 Then I use String.format("%.3f", myString); So the result is 1.234 But when my double is 10 The result will be 10,000 I want this to be 10 Is their a way to say that he only needs to show the decimals when it is "usefull"? I saw some posts about this, but that was php or c#, couldn't find something for android/java about this (maybe I don't look good). Hope you guys can help me out with this. Edit, for now I use something like this: myString.replace(",000", ""); But I think their is a more "friendly" code for this. The

C# String Format for hours and minutes from decimal

风流意气都作罢 提交于 2019-11-30 07:56:50
问题 Is there a simple string format that will take a decimal representing hours and fractions of hours and show it as hours and minutes? For example : 5.5 formatted to display 5 hrs 30 minutes. I am happy to write the code myself, however would prefer to use existing functionality if it is available 回答1: decimal t = 5.5M; Console.WriteLine(TimeSpan.FromHours((double)t).ToString()); That'll give you "05:30:00" which is pretty close. You could then format that to your desired result: var ts =

Get AM/PM for a date time in lowercase using only a datetime format

余生长醉 提交于 2019-11-30 07:49:22
I'm to get a custom DateTime format including the AM/PM designator, but I want the "AM" or "PM" to be lowercase without making the rest of of the characters lowercase. Is this possible using a single format and without using a regex? Here's what I've got right now: item.PostedOn.ToString("dddd, MMMM d, yyyy a\\t h:mmtt") An example of the output right now would be Saturday, January 31, 2009 at 1:34PM I would personally format it in two parts: the non-am/pm part, and the am/pm part with ToLower: string formatted = item.PostedOn.ToString("dddd, MMMM d, yyyy a\\t h:mm") + item.PostedOn.ToString(

undocumented CONVERT styles - datetime 23

两盒软妹~` 提交于 2019-11-30 07:39:10
Recently I stumbled upon CONVERT function style 23, which is very handy as it gives you DATE in format yyyy-mm-dd . The problem is that it's not documented in msdn! (link from SSMS help after F1 on CONVERT: http://msdn.microsoft.com/en-us/library/ms187928%28SQL.105%29.aspx ). Example: select convert( date ,'2012-01-30', 23) select convert(varchar(255), getdate(), 23) This style is very useful and I've been missing it, but my concerns are: - Is it safe to use? Is it deprecated or sneak in by mistake and may be removed in future editions / updates? - Does anybody know of other hidden styles?

String.Format(“{0:C2}”, -1234) (Currency format) treats negative numbers as positive

心不动则不痛 提交于 2019-11-30 06:36:09
I am using String.Format("{0:C2}", -1234) to format numbers. It always formats the amount to a positive number, while I want it to become $ - 1234 Am I right in saying it's putting it in brackets, i.e. it's formatting it as ($1,234.00) ? If so, I believe that's the intended behaviour for the US. However, you can create your own NumberFormatInfo which doesn't behave this way. Take an existing NumberFormatInfo which is "mostly right", call Clone() to make a mutable copy, and then set the CurrencyNegativePattern appropriately (I think you want value 2). For example: using System; using System

The simplest way of printing a portion of a char[] in C

心不动则不痛 提交于 2019-11-30 06:24:56
问题 Let's say I have a char* str = "0123456789" and I want to cut the first and the last three letters and print just the middle, what is the simplest, and safest, way of doing it? Now the trick: The portion to cut and the portion to print are of variable size, so I could have a very long char*, or a very small one. 回答1: You can use printf() , and a special format string: char *str = "0123456789"; printf("%.6s\n", str + 1); The precision in the %s conversion specifier specifies the maximum number

Why does .Net use a rounding algorithm in String.Format that is inconsistent with the default Math.Round() algorithm?

拥有回忆 提交于 2019-11-30 06:20:44
I've noticed the following inconsistency in C#/.NET. I was wondering why it is so. Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.04, Math.Round(1.04, 1)); Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.05, Math.Round(1.05, 1)); Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.06, Math.Round(1.06, 1)); Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.14, Math.Round(1.14, 1)); Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.15, Math.Round(1.15, 1)); Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.16, Math.Round(1.16, 1)); Console.WriteLine(); Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.04, Math

Python 3 bytes formatting

China☆狼群 提交于 2019-11-30 05:51:45
问题 In Python 3, one can format a string like: "{0}, {1}, {2}".format(1, 2, 3) But how to format bytes? b"{0}, {1}, {2}".format(1, 2, 3) raises AttributeError: 'bytes' object has no attribute 'format' . If there is no format method for bytes, how to do the formatting or "rewriting" of bytes? 回答1: And as of 3.5 % formatting will work for bytes , too! https://mail.python.org/pipermail/python-dev/2014-March/133621.html 回答2: Another way would be: "{0}, {1}, {2}".format(1, 2, 3).encode() Tested on