string-formatting

Format a number to always have a sign and decimal separator [duplicate]

瘦欲@ 提交于 2019-12-01 18:28:51
This question already has an answer here: Custom numeric format string to always display the sign 6 answers I want to format any number (integer or real) to a string representation which always has a sign (positive or negative) and a decimal separator , but no trailing zeroes. Some samples: 3.14 => +3.14 12.00 => +12. -78.4 => -78.4 -3.00 => -3. Is it possible with one of the default ToString() implementations, or do I need write this myself? Try something like this: double x = -12.43; string xStr = x.ToString("+0.#####;-0.#####"); But this wouldn't help to display trailing decimal point. You

How to mask string?

二次信任 提交于 2019-12-01 17:39:06
I have a string with value "1131200001103". How can I display it as a string in this format "11-312-001103" using Response.Write(value)? Thanks This produces the required result string result = Int64.Parse(s.Remove(5,2)).ToString("00-000-000000"); assuming that you want to drop 2 characters at the position of the 2 first nulls. Any reason you don't want to just use Substring ? string dashed = text.Substring(0, 2) + "-" + text.Substring(2, 3) + "-" + text.Substring(7); Or: string dashed = string.Format("{0}-{1}-{2}", text.Substring(0, 2), text.Substring(2, 3), text.Substring(7)); (I'm assuming

Why those different string formats on TimeSpan on XAML?

一笑奈何 提交于 2019-12-01 16:47:18
I'm going crazy. Can someone explain me why these string formats formatting the same thing are so different? <DataGridTextColumn Header="Max Time" IsReadOnly="True" Binding="{Binding MaxTime, StringFormat=hh\\:mm\\:ss, TargetNullValue=---}"> <DataGridTextColumn Header="Min Time" IsReadOnly="True"> <DataGridTextColumn.Binding> <Binding Path="MinTime" StringFormat="{}{0:hh':'mm':'ss}" TargetNullValue=" --- "/> </DataGridTextColumn.Binding> </DataGridTextColumn> Of course each one do not work on the other. EDIT: The more I work with WPF the more I feel it's not a mature enought product. I'm no

DisplayAttribute name with a variable, Dynamic DisplayName

我是研究僧i 提交于 2019-12-01 16:40:00
问题 Wondering if this is possible or something with this effect. public class MyModel { public string Name { get; set; } [Display(Name = String.Format("This is [0]'s phone number", Name)] public string PhoneNumber { get; set; } } I'm talking about a DisplayName with a variable in it, non static and possibly based on the models other properties. Is this possible in any way? 回答1: This isn't possible because the arguments specified for parameters of attributes must be constant values (instinctively,

Variable widths in Java's String.format method

早过忘川 提交于 2019-12-01 16:06:05
I'm working on a project in which I need to display textual trees. I'm trying to use Java's String.format method to simplify the formatting process, but I ran into trouble when trying to apply variable widths. Current I have a variable (an int) which is called depth. I try to do the following: String.format("%"+depth+"s"," ") + getOriginalText() + "\n"; However I get the following error. java.util.FormatFlagsConversionMismatchException: Conversion = s, Flags = 0 Any suggestions on how to do this, or should I just settle for loops? Thanks for the help! This works: int depth = 5; String str=

Variable widths in Java's String.format method

喜欢而已 提交于 2019-12-01 16:05:01
问题 I'm working on a project in which I need to display textual trees. I'm trying to use Java's String.format method to simplify the formatting process, but I ran into trouble when trying to apply variable widths. Current I have a variable (an int) which is called depth. I try to do the following: String.format("%"+depth+"s"," ") + getOriginalText() + "\n"; However I get the following error. java.util.FormatFlagsConversionMismatchException: Conversion = s, Flags = 0 Any suggestions on how to do

How to use string interpolation in a resource file?

不羁岁月 提交于 2019-12-01 15:57:23
问题 I would like to use a resource file to send an email. In my resource file I used a variable "EmailConfirmation" with the value "Hello {userName} ... " In my class I used: public static string message (string userName) { return Resource.WebResource.EmailConfirmation } The problem is that the return says "Hello {userName}" instead of "Hello Toto". 回答1: You can't make use of string interpolation in the context of resources. However you could achieve that you want by making use of string.Format .

Why those different string formats on TimeSpan on XAML?

馋奶兔 提交于 2019-12-01 15:51:27
问题 I'm going crazy. Can someone explain me why these string formats formatting the same thing are so different? <DataGridTextColumn Header="Max Time" IsReadOnly="True" Binding="{Binding MaxTime, StringFormat=hh\\:mm\\:ss, TargetNullValue=---}"> <DataGridTextColumn Header="Min Time" IsReadOnly="True"> <DataGridTextColumn.Binding> <Binding Path="MinTime" StringFormat="{}{0:hh':'mm':'ss}" TargetNullValue=" --- "/> </DataGridTextColumn.Binding> </DataGridTextColumn> Of course each one do not work on

Formatting output as table

久未见 提交于 2019-12-01 14:49:59
Example input: [('b', 'c', 4), ('l', 'r', 5), ('i', 'a', 6), ('c', 't', 7), ('a', '$', 8), ('n', '$', 9)] [0] contains the vertical heading, [1] contains the horizontal heading. Example output: c r a t $ $ b 4 l 5 i 6 c 7 a 8 n 9 Note: given enough tuples the entire table could be filled :P How do I format output as a table in Python using [preferably] one line of code? Here's an answer for your revised question: data = [ ['A','a','1'], ['B','b','2'], ['C','c','3'], ['D','d','4'] ] # Desired output: # # A B C D # a 1 # b 2 # c 3 # d 4 # Check data consists of colname, rowname, value triples

how format specifier taking value while tuple list is passed

别来无恙 提交于 2019-12-01 14:37:37
I have a piece of code as below: tupvalue = [('html', 96), ('css', 115), ('map', 82)] So while printing the above tuple in the desired format for a particular index I found a code like this: >>> '%s:%d' % tupvalue[0] 'html:96' I'm wondering how the single value tupvalue[0] is recognised as a tuple of two values by the format specifier '%s:%d' ? Please explain this mechanism with a documentation reference. How can I use a comprehension to format all the values in tupvalue in the required format as in the example shown? First, the easy question: How can I use a comprehension to format all the