string.format

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

烂漫一生 提交于 2019-11-29 07:22:42
问题 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 回答1: 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

Printing boolean values True/False with the format() method in Python

浪子不回头ぞ 提交于 2019-11-29 05:48:10
问题 I was trying to print a truth table for Boolean expressions. While doing this, I stumbled upon the following: >>> format(True, "") # shows True in a string representation, same as str(True) 'True' >>> format(True, "^") # centers True in the middle of the output string '1' As soon as I specify a format specifier, format() converts True to 1 . I know that bool is a subclass of int , so that True evaluates to 1 : >>> format(True, "d") # shows True in a decimal format '1' But why does using the

Python string.format() percentage to one decimal place

a 夏天 提交于 2019-11-29 05:38:37
问题 In the example below I would like to format to 1 decimal place but python seems to like rounding up the number, is there a way to make it not round the number up? >>> '{:.1%}'.format(0.9995) '100.0%' >>> '{:.2%}'.format(0.9995) '99.95%' Thanks! :) 回答1: If you want to round down always (instead of rounding to the nearest precision), then do so, explicitly, with the math.floor() function: from math import floor def floored_percentage(val, digits): val *= 10 ** (digits + 2) return '{1:.{0}f}%'

String.Format vs “string” + “string” or StringBuilder? [duplicate]

蓝咒 提交于 2019-11-29 05:37:44
Possible Duplicates: Is String.Format as efficient as StringBuilder C# String output: format or concat? What is the performance priority and what should be the conditions to prefer each of the following: String.Format("{0}, {1}", city, state); or city + ", " + state; or StringBuilder sb = new StringBuilder(); sb.Append(city); sb.Append(", "); sb.Append(state); sb.ToString(); Compiler will optimize as much string concat as it can, so for example strings that are just broken up for line break purposes can usually be optimized into a single string literal. Concatenation with variables will get

Escaping single quote in String.Format()

会有一股神秘感。 提交于 2019-11-29 01:07:32
I have been all over the 'tubes and I can't figure this one out. Might be simple. The following String.Format call: return dt.ToString("MMM d yy 'at' H:mmm"); Correctly returns this: Sep 23 08 at 12:57 Now let's say I want to add a single quote before the year, to return this: Sep 23 '08 at 12:57 Since the single quote is a reserved escape character, how do I escape the single quote to get it to display? I have tried double, triple, and quad single quotes, with no luck. You can escape it using a backslash which you will have to escape. Either return dt.ToString(@"MMM d \'yy 'at' H:mmm"); or

Why do the overloads of String.Format exist?

被刻印的时光 ゝ 提交于 2019-11-28 11:56:40
I was using Reflector to look at the implementation of String.Format and had always been under the impression that the overloads of String.Format that took 1, 2 & 3 arguments were optimized versions of the method that takes an object array. However, what I found was that internally they create an object array and then call a method that takes an object array. 1 arg public static string Format(string format, object arg0) { if (format == null) { throw new ArgumentNullException("format"); } return Format(null, format, new object[] { arg0 }); } 2 args public static string Format(string format,

How can I left-align strings using String.format()?

蓝咒 提交于 2019-11-28 06:41:56
I'm using String.format() in Java trying to emulate the printf() control channel available in C. I understand how to specify that a string should be placed in a field which takes 20 characters, 5, 2 ... with 3 decimals, 2, etc. However, the strings are printed right-aligned in their field. How do I left-align the strings? Here's an example of a possible output which I would like to modify to left-align EXECUTING and CREATED in their fields. Process PID: 25 Status: ----------- EXECUTING Process PID: 36 Status: READY-SUSPENDED Process PID: 4 Status: ---------------- CREATED *note: consider '-'

Safer compile-time String.format() alternative issue 2

北慕城南 提交于 2019-11-28 06:18:04
问题 With String.format , there seems to be a large opening for programmatic error that isn't found at compile-time. This can make fixing errors more complex and / or take longer. This was the issue for me that I set out to fix (or hack a solution). I came close, but I am not close enough. For this problem, this is more certainly over-engineered. I understand that, but I just want to find a good compile-time solution to this. More information can be found here. My question dealing with Basic

How can I escape the format string?

喜夏-厌秋 提交于 2019-11-28 03:54:36
问题 Is it possible to use Python's str.format(key=value) syntax to replace only certain keys. Consider this example: my_string = 'Hello {name}, my name is {my_name}!' my_string = my_string.format(name='minerz029') which returns KeyError: 'my_name' Is there a way to achieve this? 回答1: You can escape my_name using double curly brackets, like this >>> my_string = 'Hello {name}, my name is {{my_name}}!' >>> my_string.format(name='minerz029') 'Hello minerz029, my name is {my_name}!' As you can see,

How to use string.Format() to format a hex number surrounded by curly brackets?

依然范特西╮ 提交于 2019-11-28 02:48:21
问题 Input: uint hex = 0xdeadbeef; Required output: string result = "{deadbeef}" First approach: Explicitly add the { and } ; this works: result = "{" + string.Format("{0:x}", hex) + "}"; // -> "{deadbeef}" Output as decimal rather than hex using escaped curly brackets: result = string.Format("{{{0}}}", hex); // -> "{3735928559}" Seems promising, now all we need to do is add the :x hex specifer as per the first approach above: result = string.Format("{{{0:x}}}", hex); // -> "{x}" Oh dear, adding