string-formatting

WPF Generate TextBlock Inlines

余生长醉 提交于 2019-11-28 09:52:01
I have a GridView and in one of the GridViewColumn s i want to generate a text like this: textBlock.Text = string.Format("{0} is doing {1} .......", a, b); but a and b (Properties of an item in the View) should not just be represented as plain text, but as a Hyperlink for example. (Also: The format text should depend on the type of the item) How can i generate the TextBlock s text in that way? (for localization) The Question is more: Should i write something on my own or is there an easy way provided by the framework? Recently i came across the same problem. So i decided to implement an

WPF binding StringFormat syntax

喜欢而已 提交于 2019-11-28 09:45:34
How can I format a decimal value conditionally in a WPF window? Value should be rounded to a whole number (Ex: 1,234) When the value is 0.00, it should display as a single zero. (Ex: 0) Currently I use bellow mark up to format the decimal value, but it displays 00 when the value is 0.00. Please help. <TextBlock Grid.Column="6" Padding="2" Text="{Binding Path=TotalAwardsExpended, StringFormat='{}{0:0,0}'}" /> The extra 0 comes from the 0 after the colon. Instead, try {}{0:#,0} . From the MSDN docs on Custom Numeric String formats (emphasis added): "0" | Zero placeholder | Replaces the zero with

Python string formatting: % vs concatenation

帅比萌擦擦* 提交于 2019-11-28 08:36:45
I'm developing an application in which I perform some requests to get an object id. After each one of them, I call a method ( get_actor_info() ) passing this id as an argument (see code below). ACTOR_CACHE_KEY_PREFIX = 'actor_' def get_actor_info(actor_id): cache_key = ACTOR_CACHE_KEY_PREFIX + str(actor_id) As can be noticed, I'm casting actor_id to string and concatenating it with a prefix. However, I know I could do it in multiple other ways ( .format() or '%s%d' , for instance) and that results in my question: would '%s%d' be better than string concatenation in terms of readability, code

Plural String Formatting

六月ゝ 毕业季﹏ 提交于 2019-11-28 07:27:53
Given a dictionary of int s, I'm trying to format a string with each number, and a pluralization of the item. Sample input dict : data = {'tree': 1, 'bush': 2, 'flower': 3, 'cactus': 0} Sample output str : 'My garden has 1 tree, 2 bushes, 3 flowers, and 0 cacti' It needs to work with an arbitrary format string. The best solution I've come up with is a PluralItem class to store two attributes, n (the original value), and s (the string 's' if plural, empty string '' if not). Subclassed for different pluralization methods class PluralItem(object): def __init__(self, num): self.n = num self._get_s

Easiest way to format a number with thousand separators to an NSString according to the Locale

丶灬走出姿态 提交于 2019-11-28 07:20:38
I can't seem to find an easy way to do it. The exact thing I need is: [NSString stringWithFormat:@"%d doodads", n]; Where n is an int. So for 1234 I'd want this string (under my locale): @"1,234 doodads" Thanks. Todd Ransom For 10.6 this works: NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter setFormatterBehavior: NSNumberFormatterBehavior10_4]; [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle]; NSString *numberString = [numberFormatter stringFromNumber: [NSNumber numberWithInteger: i]]; And it properly handles localization. I have recently

Python 3.2: How to pass a dictionary into str.format()

被刻印的时光 ゝ 提交于 2019-11-28 07:19:21
问题 I've been reading the Python 3.2 docs about string formatting but it hasn't really helped me with this particular problem. Here is what I'm trying to do: stats = { 'copied': 5, 'skipped': 14 } print( 'Copied: {copied}, Skipped: {skipped}'.format( stats ) ) The above code will not work because the format() call is not reading the dictionary values and using those in place of my format placeholders. How can I modify my code to work with my dictionary? 回答1: This does the job: stats = { 'copied':

Longest common prefix of two strings in bash

半世苍凉 提交于 2019-11-28 06:47:37
I have two strings. For the sake of the example they are set like this: string1="test toast" string2="test test" What I want is to find the overlap starting at the beginning of the strings. With overlap I mean the string "test t" in my above example. # So I look for the command command "$string1" "$string2" # that outputs: "test t" If the strings were string1="atest toast"; string2="test test" they would have no overlap since the check starts form the beginning and the "a" at the start of string1 . jfg956 In sed, assuming the strings don't contain any newline characters: string1="test toast"

Formatting binary values in Scala

梦想的初衷 提交于 2019-11-28 06:46:43
Does Scala have a built in formatter for binary data? For example to print out: 00000011 for the Int value 3. Writing one won't be difficult - just curious if it exists. Lauri scala> 3.toBinaryString res0: String = 11 Scala has an implicit conversion from Int to RichInt which has a method toBinaryString. This function does not print the leading zeroes though. I don't know of a direct API method to do it, but here is one way of doing it: def toBinary(i: Int, digits: Int = 8) = String.format("%" + digits + "s", i.toBinaryString).replace(' ', '0') 8 digits for number 3 with leading zeros: printf

python format string thousand separator with spaces

落花浮王杯 提交于 2019-11-28 06:42:13
For printing number with thousand separator, one can use the python format string : '{:,}'.format(1234567890) But how can I specify that I want a space for thousands separator? Here is bad but simple solution if you don't want to mess with locale : '{:,}'.format(1234567890.001).replace(',', ' ') Answer of @user136036 is quite good, but unfortunately it does not take into account reality of Python bugs. Full answer could be following: Variant A If locale of your platform is working right, then just use locale: import locale locale.setlocale(locale.LC_ALL, '') print("{:,d}".format(7123001))

“%s” % format vs “{0}”.format() vs “?” format

耗尽温柔 提交于 2019-11-28 06:31:21
In this post about SQLite , aaronasterling told me that cmd = "attach \"%s\" as toMerge" % "b.db" : is wrong cmd = 'attach "{0}" as toMerge'.format("b.db") : is correct cmd = "attach ? as toMerge"; cursor.execute(cmd, ('b.db', )) : is right thing But, I've thought the first and second are the same. What are the differences between those three? "attach \"%s\" as toMerge" % "b.db" You should use ' instead of " , so you don't have to escape. You used the old formatting strings that are deprecated. 'attach "{0}" as toMerge'.format("b.db") This uses the new format string feature from newer Python