string-formatting

Java String.format() with HALF_EVEN rounding

情到浓时终转凉″ 提交于 2019-12-24 02:17:15
问题 I'd like to use String.format() to format some BigDecimals as part of a string: // Example: String getPrice( String pattern ) { BigDecimal price = basePrice.multiply( BigDecimal.ONE.add( vatRate ) ); BigDecimal priceInPence = price.multiply( new BigDecimal( "100" ) ); BigDecimal annualPrice = price.multiply( new BigDecimal( "365" ) ); return String.format( pattern, priceInPence, annualPrice ); } String myPrice1 = getPrice( "Your price is %1$.3fp/day (£2$.2f/year) including VAT" ); // -->

Why isn't it possible to use backslashes in f-strings?

蹲街弑〆低调 提交于 2019-12-23 22:42:48
问题 In Python >=3.6, f-strings can be used as a replacement for the str.format method. As a simple example, these are equivalent: '{} {}'.format(2+2, "hey") f'{2+2} {"hey"}' Disregarding format specifiers, I can basically move the positional arguments of str.format inside braces in an f-string. Note specifically that I am allowed to just put str literals in here, although it may seem a bit unwieldy. There are however some limitations. Specifically, backslashes in any shape or form are disallowed

WPF - ToolTip Binding with stringformat

这一生的挚爱 提交于 2019-12-23 22:19:59
问题 I want to make a tool tip using binding and string format, he is what I've tried: <TextBlock Text="{Binding Name}" ToolTip="{Binding Path=Name, StringFormat='The name is: {0}{}'}"/> The value of Name is : Golan What I expected to see is: The name is: Golan But all is see is: Golan 回答1: I think you can use this code <TextBlock Width="100" x:Name="tt" Text="{Binding Name}"> <TextBlock.ToolTip> <ToolTip Width="100" Content="{Binding Name}" ContentStringFormat="The Name is: {0}" /> </TextBlock

What does -Wformat=2 do?

守給你的承諾、 提交于 2019-12-23 11:01:13
问题 I'm seeing numerous mentions on the web of a -Wformat=2 option to Clang, sometimes alongside the already-known -Wformat (the one that Xcode lists as “Typecheck calls to printf / scanf , although it covers many more string-formatting APIs now). Does this do anything at all? If it does, what, if anything, does it do differently from -Wformat ? Is it useful to have both, or is either one a superset of the other? 回答1: If it does, what, if anything, does it do differently from -Wformat? It's

Format number number with specific mask regex python

耗尽温柔 提交于 2019-12-23 09:32:46
问题 I need to format a number with a specifc mask: 9.9.9.9.99.999 , depending on the length of number string. For example: - 123456789 => 1.2.3.4.56.789 - 123456 => 1.2.3.4.56 - 1234 => 1.2.3.4 - 123 => 1.2.3 - 12 => 1.2 It will not occur a number string with 7 or 8 digits in the input. How could that be implemented with regex, preferably in python? Thanks in advance. 回答1: You can use this pattern: (?:(?<=^\d)|(?<=^\d{2})|(?<=^\d{3})|(?<=^\d{4})|(?<=^\d{6}))(?=\d) with . as replacement. example:

What should I use instead of printf in Perl?

亡梦爱人 提交于 2019-12-23 09:29:45
问题 I need to use some string replacement in Perl to ease translations, i.e. replace many print "Outputting " . $n . " numbers"; by something like printf ("Outputting %d numbers", $n); However, I'd like to replace printf with something easier to parse for humans, like this: printX ("Outputting {num} numbers", { num => $n }); or generally something more Perly. Can you recommend something (from CPAN or not) you like and use? 回答1: Most Templating modules on CPAN will probably do what you want. Here

Lua string.format options

别等时光非礼了梦想. 提交于 2019-12-23 07:27:49
问题 This may seem like a stupid question, but what are the symbols used for string replacement in string.format? can someone point me to a simple example of how to use it? 回答1: string.format in Lua follows the same patterns as Printf in c: http://www.cplusplus.com/reference/clibrary/cstdio/printf/ There are some exceptions, for those see here: http://pgl.yoyo.org/luai/i/string.format 回答2: Chapter 20 of PiL describes string.format near the end: The function string.format is a powerful tool when

How do I escape a colon in Python format() when using kwargs?

拜拜、爱过 提交于 2019-12-23 07:15:39
问题 I have a dictionary with a colon in a key that I wish to print. Unfortunately the colon character is used for formatting, so I need to somehow escape it. For example: >>> d = {'hello': 'world', 'with:colon': 'moo'} >>> '{hello}'.format(**d) 'world' >>> '{with:colon}'.format(**d) KeyError: 'with' >>> '{with\:colon}'.format(**d) KeyError: 'with\\' >>> '{with::colon}'.format(**d) KeyError: 'with' 回答1: As a workaround: >>> d = {'hello': 'world', 'with:colon': 'moo'} >>> '{hello} {}'.format(d[

String format in .NET: convert integer to fixed width string?

末鹿安然 提交于 2019-12-23 06:47:32
问题 I have an int in .NET/C# that I want to convert to a specifically formatted string. If the value is 1, I want the string to be "001". 10 = "010". 116 = "116". etc... I'm looking around at string formatting, but so far no success. I also won't have values over 999. 回答1: Take a look at PadLeft. ex: int i = 40; string s = i.ToString().PadLeft(3, '0'); s == "040" 回答2: The simplest way to do this is use .NET's built-in functionality for this: var r = 10; var p = r.ToString("000"); No need for

Why does the NSString class round inconsistently when formatting numbers?

做~自己de王妃 提交于 2019-12-23 01:41:57
问题 The output of the following two lines seem to show that NSString's stringWithFormat statement does not round numbers consistently. It seems to round up correctly from 0.75 to 0.8. But it rounds 0.25 down to 0.2. Here is the code. NSString *sRoundedScore = [NSString stringWithFormat:@"%.1f", fScore]; NSLog(@"\r\n score before rounding: %f, rounded score: %@", fScore, sRoundedScore); When fScore is assigned to be 0.25, the output is: score before rounding: 0.250000, rounded score: 0.2 When