string-formatting

StringFormat in XAML

一个人想着一个人 提交于 2019-12-07 02:30:17
问题 I am trying to format my string to have commas every 3 places, and a decimal if it is not a whole number. I have checked roughly 20 examples, and this is the closest I have come: <TextBlock x:Name="countTextBlock" Text="{Binding Count, StringFormat={0:n}}" /> But I get a The property 'StringFormat' was not found in type 'Binding'. error. Any ideas what is wrong here? Windows Phone 8.1 appears to differ from WPF, because all of the WPF resources say that this is how it is done. (The string is

Using .NET string formatting, how do I format a string to display blank (empty string) for zero (0)?

僤鯓⒐⒋嵵緔 提交于 2019-12-07 02:28:45
问题 I am using a DataBinder.Eval expression in an ASP.NET Datagrid, but I think this question applies to String formatting in .NET in general. The customer has requested that if the value of a string is 0, it should not be displayed. I have the following hack to accomplish this: <%# IIf(DataBinder.Eval(Container.DataItem, "MSDWhole").Trim = "0", "", DataBinder.Eval(Container.DataItem, "MSDWhole", "{0:N0}")) %> I would like to change the {0:N0} formatting expression so that I can eliminate the IIf

How to format std::chrono durations?

落花浮王杯 提交于 2019-12-07 01:42:29
问题 Is there a convenient way to format std::chrono::duration to a specified format? std::chrono::high_resolution_clock::time_point now, then; then = std::chrono::high_resolution_clock::now(); // ... now = std::chrono::high_resolution_clock::now(); auto duration = now - then; // base in microseconds: auto timeInMicroSec = std::chrono::duration_cast<std::chrono::microseconds>(duration); How can I format timeInMicroSec like ss::ms::us ? 回答1: One can use something like: #include <iomanip> #include

How do I format a floating point value so that it never uses exponent notation nor has trailing zeros?

风流意气都作罢 提交于 2019-12-07 00:33:17
问题 According to ios_base manipulators, I basically have the choice between defaultfloat and fixed when formatting floating point numbers without exponent notation (with decimal numbers). However, I want to choose the maximum precision which would produce a lot trailing zeros for fixed for many numbers (e.g. 1. ) but avoid ever using the exponent notation. If set to defaultfloat , it will look right most of the time, unless the value is really really small, yet not 0. . In that case, the default

Formatting columns containing non-ascii characters

假如想象 提交于 2019-12-06 23:49:50
问题 So I want to align fields containing non-ascii characters. The following does not seem to work: for word1, word2 in [['hello', 'world'], ['こんにちは', '世界']]: print "{:<20} {:<20}".format(word1, word2) hello world こんにちは 世界 Is there a solution? 回答1: You are formatting a multi-byte encoded string. You appear to be using UTF-8 to encode your text and that encoding uses multiple bytes per codepoint (between 1 and 4 depending on the specific character). Formatting a string counts bytes , not

.NET GUID uppercase string format

跟風遠走 提交于 2019-12-06 20:28:58
问题 I need to format my GUIDs in the dashed format, all uppercase. I know using myGuid.ToString("D") or String.Format("{0:D}", myGuid) gives the dashed format, but using an uppercase D as opposed to a lower-case d doesn't give me an uppercased GUID like I thought it would. Is there a way to do this without doing anything crazy, or do I just need to call myGuid.ToString().ToUpper() ? 回答1: do I just need to call myGuid.ToString().ToUpper() Yep. You could go to the effort of creating a custom

Default kwarg values for Python's str.format() method

僤鯓⒐⒋嵵緔 提交于 2019-12-06 18:31:28
问题 I'm looking to try and keep pluralisation of existing strings as simple as possible, and was wondering if it was possible to get str.format() to interpret a default value when looking for kwargs. Here's an example: string = "{number_of_sheep} sheep {has} run away" dict_compiled_somewhere_else = {'number_of_sheep' : 4, 'has' : 'have'} string.format(**dict_compiled_somewhere_else) # gives "4 sheep have run away" other_dict = {'number_of_sheep' : 1} string.format(**other_dict) # gives a key

How is C# string interpolation compiled?

你。 提交于 2019-12-06 18:30:06
问题 I know that interpolation is syntactic sugar for string.Format() , but does it have any special behavior/recognition of when it is being used with a string formatting method? If I have a method: void Print(string format, params object[] parameters) And the following call to it using interpolation: Print($"{foo} {bar}"); Which of the following calls lines is most equivalent to the compiled result of string interpolation? Print(string.Format("{0} {1}", new[] { foo, bar })); Print("{0} {1}", new

Remove all occurrences of several chars from a string

北城以北 提交于 2019-12-06 17:25:38
问题 Is there a pythonic way to do what the str.strip() method does, except for all occurrences, not just those at the beginning and end of a string? Example: >> '::2012-05-14 18:10:20.856000::'.strip(' -.:') >> '2012-05-14 18:10:20.856000' I want >> '::2012-05-14 18:10:20.856000::'.crazy_function(' -.:') >> '20120514181020856000' Does Python provides me a built-in crazy_function ??? I could easily do it programatically, but I want to know if there is a built-in for that. Couldn't find one. Thank

Why does percentage format specifier multiply by 100?

别来无恙 提交于 2019-12-06 17:07:56
问题 Why does the 'p' string format for percent multiply the value by 100 before formatting it with the percentage sign? http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#PFormatString The Percent ("P") Format Specifier The percent ("P") format specifier multiplies a number by 100 and converts it to a string that represents a percentage. The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the default numeric precision supplied by the