string-formatting

Refer to the same parameter multiple times in a fmt.Sprintf format string

南楼画角 提交于 2019-12-05 20:40:12
I have this function: func getTableCreationCommands(v string) string { return ` CREATE TABLE share_` + v + ` PARTITION OF share FOR VALUES IN (` + v + `); CREATE TABLE nearby_` + v + ` PARTITION OF nearby FOR VALUES IN (` + v + `); ` } It's a little wonky... is there a way to format the string using fmt.Sprintf ? Something like this: func getTableCreationCommands(v string) string { return fmt.Sprintf(` CREATE TABLE share_%v PARTITION OF share FOR VALUES IN (%v); CREATE TABLE nearby_%v PARTITION OF nearby FOR VALUES IN (%v); `, v, v, v, v) } but without the need to pass v 4 times? Package fmt

Why can't Python's string.format pad with “\x00”?

自作多情 提交于 2019-12-05 20:38:30
问题 I wanted to pad a string with null characters ("\x00"). I know lots of ways to do this, so please do not answer with alternatives. What I want to know is: Why does Python's string.format() function not allow padding with nulls? Test cases: >>> "{0:\x01<10}".format("bbb") 'bbb\x01\x01\x01\x01\x01\x01\x01' This shows that hex-escaped characters work in general. >>> "{0:\x00<10}".format("bbb") 'bbb ' But "\x00" gets turned into a space ("\x20"). >>> "{0:{1}<10}".format("bbb","\x00") 'bbb ' >>> "

Python string formatting - old `%` vs new `str.format`

隐身守侯 提交于 2019-12-05 20:34:55
问题 New formatting lets us do this: '{:.<12}'.format('##') - optional fill character. Can we do that using old formatting? (I know we can fill with spaces '%-12s' % '##' ) Also, old formatting lets us do this: '%-*s' % (12, '##') - variable length. Can we do that using new formatting? 回答1: For doing variable length using new-format , you can use nesting of replacements - >>> '{:{}<{}}'.format('##','.',12) '##..........' >>> '{:{}<{}}'.format('##','-',12) '##----------' >>> '{:{}<{}}'.format('##',

Best way to achieve complicated numeric formatting in C#

浪子不回头ぞ 提交于 2019-12-05 20:12:42
What is the best way to replicate the behaviour of something like this in C#? // Converts decimal to a 0-padded string with minimum specified width and precision. sprintf(out, "%0*.*lf", width, precision, decimal); I had a read of standard , custom and composite format strings, but I can't see any nice way to achieve this. The reason I ask is that I just came across this ugly snippet in some code I am maintaining where we are required to format a bunch of decimals according to variable widths and precisions as specified by an external interface: private static String ZEROS =

Precise definition of float string formatting?

孤人 提交于 2019-12-05 19:30:32
Is the following behavior defined in Python's documentation (Python 2.7)? >>> '{:20}'.format(1e10) ' 10000000000.0' >>> '{:20g}'.format(1e10) ' 1e+10' In fact, the first result surprises me: the documentation indicates that not indicating the format type ('f', 'e', etc.) for floats is equivalent to using the general format 'g'. This example shows that this does not seem to be the case, so I'm confused. Maybe this is related to the fact that "A general convention is that an empty format string ("") produces the same result as if you had called str() on the value."? In fact: >>> str(1e10)

Which of one from string interpolation and string.format is better in performance?

南楼画角 提交于 2019-12-05 18:51:51
问题 Consider this code: var url = "www.example.com"; String.Format: var targetUrl = string.Format("URL: {0}", url); String Interpolation: var targetUrl=$"URL: {url}"; Which of one from string interpolation and string.Format is better in performance? Also what are the best fit scenarios for use of them? 回答1: Which of one from string interpolation and string.format is better in performance? Neither of them is better since they are equal on run-time. String interpolation is rewritten by the compiler

Issue with StringFormat for small negative numbers that have been rounded

随声附和 提交于 2019-12-05 18:41:43
I am trying to write a string format that will round or pad numbers to two decimal places, and also display negative numbers with brackets around them. This is in a in a WPF application, but it applies to any .net StringFormat use case. For example: 2.0 -> 2.00 -2.0 -> (2.00) 0.01 -> 0.01 -0.01 -> (0.01) 0.001 -> 0.00 -0.001 -> (0.00) The last one, a very small negative number rounded to zero, is the problem. I still want the brackets to indicate that it was negative. My first version was a string format as follows: {0:#,##0.00 ;(#,##0.00)} This doesn't work, because it rounds the number

Date formatting with locale in Play Framework 2.2

别等时光非礼了梦想. 提交于 2019-12-05 18:23:38
From what I can see in the auto-generated application.conf file, dates/times in Play Framework 2.2 are formatted according to the definition of date.format in that file. I have, for instance, defined date.format=yyyy-MM-dd date.format.dk=d. MMMM yyyy These values, however, seem to be ignored by the framework when printing dates in Scala templates. This thread gives a solution where one enters the pattern directly into the template as myDate.format("yyyy-MM-dd") . (If using Jodatime I guess this becomes myDate.toDate().format("yyyy-MM-dd") since there is no format() defined on the DateTime

scanf format to ignore irrelevant characters

本秂侑毒 提交于 2019-12-05 17:07:08
I wrote a short example code to illustrate my problem #include <stdio.h> #include <string.h> unsigned parseAndCompareDouble(const char* inSTR, const char* inF, const char * expect, const char * outF){ unsigned e = 0; char buffer[2000]; double a = 0; if( 1 != sscanf_s( inSTR, inF, &a, sizeof(double) ) ) e += 1; if( (int) strlen(expect) != sprintf_s(buffer, 2000, outF, a) ) e += 1; if( 0 != strcmp(expect, buffer) ) e += 1; return e; } unsigned main( void ) { unsigned e = 0; const char * universalFormat = "X/%lf"; e += parseAndCompareDouble("X/100", universalFormat, "X/100", "X/%3.0lf"); e +=

Which log4j facade to choose?

五迷三道 提交于 2019-12-05 16:07:55
Essentially I'm looking for something with the same behavior, configuration, logging levels as log4j, but with some of the missing functionality (e.g. formatted logging — see here and here for related SO threads.) Current nominees are slf4j and log5j . I'm inclined toward SLF4J. allows parameterized logging (formatting) allows consolidation of other frameworks (great for apps using many libraries, each logging to a different framework) log5j is good, but does not have near as much market penetration. since a lot of frameworks already support slf4j you will only have to setup your logging once