string-formatting

Elegant Solutions to the Fencepost Problem (with Strings)

我只是一个虾纸丫 提交于 2019-12-10 21:59:16
问题 What I'm referring to is concatenating String s with a certain String in the middle, such as concatenating sentences separated by a period, or parameter lists with a comma. I know you can use libraries, but sometimes these can't do what you want, like when you want to generate the phrases you are concatenating. So far I've come up with two solutions, StringBuffer sentence = new StringBuffer(); String period = ""; for ( int i = 0; i < sentences.length; i++ ) { sentence.append( period +

Display Currency in two decimals, but keeping all available decimals

不问归期 提交于 2019-12-10 19:00:23
问题 Quick(I hope) question. I've got this DataGrid, containing a few currency column's. What I can't seem to find is how to display the currency in 2 decimals. But maintaining a possible 3 or 4 decimal value in the cell. If tried some string-formatting but that actually changes the value. And so does a IValueConverter of course. So what am I missing? or could anybody point me in the right direction? EDIT: after the suggestion I tried something like this. <Style x:Key="DecimalTextBlockStyle"

Binding String Format Number Commas & No Decimal Places

℡╲_俬逩灬. 提交于 2019-12-10 17:54:17
问题 Okay this is an easy one but I'd hugely appreciate your help because I've been mucking around for an hour trying to get it to work. How do I change the following to get rid of the decimal places and just show the whole number? Binding="{Binding ANLA, StringFormat=n}" I know the format is something like {0:0,0} But I can't get the backslashes to work. Thanks hugely in advance! 回答1: You almost had it... how about trying this: {Binding ANLA, StringFormat='0,0.'} Please note that this will round

Automatic conversion of the advanced string formatter from the old style

匆匆过客 提交于 2019-12-10 17:51:18
问题 Is there any automatic way to convert a piece of code from python's old style string formatting (using % ) to the new style (using .format )? For example, consider the formatting of a PDB atom specification: spec = "%-6s%5d %4s%1s%3s %1s%4d%1s %8.3f%8.3f%8.3f%6.2f%6.2f %2s%2s" I've been converting some of these specifications by hand as needed, but this is both error prone, and time-consuming as I have many such specifications. 回答1: Use pyupgrade pyupgrade --py3-plus <filename> You can

StringFormat Binding with Apostrophe in XAML Not Working

走远了吗. 提交于 2019-12-10 17:46:22
问题 I'm working in Silverlight 4 and I'm trying to insert an apostrophe in a value that is bound to a TextBlock: <TextBlock Text="{Binding MyValue, StringFormat='The value is &apos;{0}&apos;'}"/> However, I'm getting XAML parse errors even though I have tried escaping with it with \' and " to no success. 回答1: This will work in WPF or Silverlight. <Grid> <Grid.Resources> <system:String x:Key="Format">The value is '{0}'</system:String> </Grid.Resources> <TextBlock Text="{Binding MyValue,

How to set the spaces in a string format in Python 3

时间秒杀一切 提交于 2019-12-10 17:33:23
问题 How can I set up the string format so that I could use a variable to ensure the length can change as needed? For example, lets say the length was 10 at first, then the input changes then length becomes 15. How would I get the format string to update? length = 0 for i in self.rows: for j in i: if len(j) > length: length = len(j) print('% length s') Obviously the syntax above is wrong but I can't figure out how to get this to work. 回答1: Using str.format >>> length = 20 >>> string = "some string

String formatting: negative/positive floating point numbers

徘徊边缘 提交于 2019-12-10 17:24:02
问题 How can I use String.Format in C# so doubles are displayed like this: example: ___-1.000 ____1.000 __100.123 -1000.321 _1000.214 etc... Where _ is space ( " " ); All I can do is String.Format("{0:F3}", -123.321); 回答1: You can use alignment: String.Format("{0,10:F3}", -123.321) where 10 is preffered length. See Composite Formatting. 回答2: Found a quick article, in short: String.Format("{0,10:0.0}", 123.4567); // " 123.5" String.Format("{0,-10:0.0}", 123.4567); // "123.5 " String.Format("{0,10:0

Change Date Format to ddth mmm,yyyy

梦想的初衷 提交于 2019-12-10 17:18:58
问题 I am printing some dates on my webform. Currently my Date Format is dd:mmm:yyyy hh:mm How can I change the date format to ddth mmm,yyyy for example 17th May,2016 hh:mm Here is my Code : lastlogin = DateTime.Parse(dt_LastLoginDetail.Rows[0]["login_time"].ToString()); lastlogindate = "Last Login Date: " + lastlogin.ToString("dd-MMM-yyy hh:mm tt"); 回答1: Try this: lastlogin = DateTime.Parse(dt_LastLoginDetail.Rows[0]["login_time"].ToString()); string suffix; switch (lastlogin.Day) { case 1: case

How to fix the precision with the `n` format

本秂侑毒 提交于 2019-12-10 17:12:36
问题 I want to print a decimal using a comma as decimal separator. When I do this import locale locale.setlocale(locale.LC_ALL, 'nl_NL') '{0:#.2n}'.format(1.1) I get '1,1' . The comma is there, but the precision is only one, whereas I set it to two. How come? Note this format is constructed as follows: # : "The '#' option causes the "alternate form" to be used for the conversion. ... In addition, for 'g' and 'G' conversions, trailing zeros are not removed from the result." .2 : Precision. n :

printf seems to be ignoring string precision

那年仲夏 提交于 2019-12-10 17:04:22
问题 So, I'm a bit stymied. According to man 3 printf on my system, the string format "%5s" should use the specified precision to limit the number of characters printed from the string argument given. % man 3 printf PRINTF(3) BSD Library Functions Manual PRINTF(3) NAME printf, fprintf, sprintf, snprintf, asprintf, vprintf, vfprintf, vsprintf, vsnprintf, vasprintf -- formatted output conversion ... s The char * argument is expected to be a pointer to an array of character type (pointer to a string)