string-formatting

How to use python str.format inside a string of json format

南笙酒味 提交于 2019-12-03 12:40:11
Python Version 3.5 I'm trying to make an API call to configure a device using json as the format. Some of the json will vary depending on the desired naming, so I need to call a variable in the string. I am able to accomplish this using the old style %s... % (variable) , but not with the new style {}... .format(variable) . Failed EX: (Testing with {"fvAp":{"attributes":{"name":(variable)}}}) a = "\"app-name\"" app_config = ''' { "fvAp": { "attributes": { "name": {} }, "children": [ { "fvAEPg": { "attributes": { "name": "app" }, "children": [ { "fvRsBd": { "attributes": { "tnFvBDName": "default

Format string with trailing zeros removed for x decimal places in Swift [duplicate]

白昼怎懂夜的黑 提交于 2019-12-03 12:38:01
This question already has answers here : Formatting decimal places with unknown number (4 answers) This in Swift (1.2) let doubleValue1 = Double(10.116983123) println(String(format: "%.2f", doubleValue1)) let doubleValue2 = Double(10.0) println(String(format: "%.2f", doubleValue2)) Prints 10.12 10.00 I'm looking for a way using a formatter or a direct string format and not via string manipulation, to remove the trailing zeroes, so to print: 10.12 10 The closest I got is: let doubleValue3 = Double(10.0) println(String(format: "%.4g", doubleValue3)) But g uses significant digits, which means I

Preventing decimals with NSNumberFormatter

送分小仙女□ 提交于 2019-12-03 12:13:20
问题 I have an NSNumberFormatter which I'm trying to use to generate a whole number of GBP (£) from an NSNumber. I keep getting two decimal places regardless of which incantation I try. My code is: NSNumberFormatter *fmtCurrency = [[[NSNumberFormatter alloc] init] autorelease]; [fmtCurrency setNumberStyle: NSNumberFormatterCurrencyStyle]; [fmtCurrency setGeneratesDecimalNumbers:FALSE]; [fmtCurrency setCurrencyCode:@"GBP"]; [fmtCurrency setCurrencySymbol:@"£"]; txtTotal.text = [fmtCurrency

Adding an extension method to the string class - C#

不问归期 提交于 2019-12-03 11:42:57
问题 Not sure what I'm doing wrong here. The extension method is not recognized. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using StringExtensions; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { RunTests(); } static void RunTests() { try { ///SafeFormat SafeFormat("Hi There"); SafeFormat("test {0}", "value"); SafeFormat("test missing second value {0} - {1}", "test1"); SafeFormat("{0}

What does “%1$#” mean when used in String.format (Java)?

坚强是说给别人听的谎言 提交于 2019-12-03 11:34:07
问题 Language is Java. What does the %1$# mean in... static String padright (String str, int num) { return String.format("%1$#" + num + "str", str); } In the Java API, String.format() is used in this way: public static String format(String format, Object... args) So I think %1$# is a format specifier. %[flags][width][.precision][argsize]typechar is the template. 1 is a flag? $ is the width? # is the precision? num is the argsize? "str" is the typechar? Is that right? 回答1: Template: %[argument

What are some good non-English phrases with singular and plural forms that can be used to test an internationalization and localization library?

删除回忆录丶 提交于 2019-12-03 10:48:08
问题 I've been working on a .NET library to assist with internationalization of an application. It's written in C#, called SmartFormat , and is open-source on GitHub. It contains Grammatical Number rules for multiple languages that determine the correct singular/plural form based on the template and locale. The rules were obtained from Unicode Language Plural Rules. When translating a phrase that has words that depend on a quantity (such as nouns, verbs, and adjectives), you specify the multiple

Verify if String matches a format String

回眸只為那壹抹淺笑 提交于 2019-12-03 10:37:38
In Java, how can you determine if a String matches a format string (ie: song%03d.mp3 )? In other words, how would you implement the following function? /** * @return true if formatted equals String.format(format, something), false otherwise. **/ boolean matches(String formatted, String format); Examples: matches("hello world!", "hello %s!"); // true matches("song001.mp3", "song%03d.mp3"); // true matches("potato", "song%03d.mp3"); // false Maybe there's a way to convert a format string into a regex? Clarification The format String is a parameter. I don't know it in advance. song%03d.mp3 is

Is there a way to build a Java String using an SLF4J-style formatting function?

允我心安 提交于 2019-12-03 10:34:35
I've heard that using StringBuilder is faster than using string concatenation, but I'm tired of wrestling with StringBuilder objects all of the time. I was recently exposed to the SLF4J logging library and I love the "just do the right thing" simplicity of its formatting when compared with String.format. Is there a library out there that would allow me to write something like: int myInteger = 42; MyObject myObject = new MyObject(); // Overrides toString() String result = CoolFormatingLibrary.format("Simple way to format {} and {}", myInteger, myObject); Also, is there any reason (including

Use str.format() to access object attributes

a 夏天 提交于 2019-12-03 10:22:23
I have a Python object with attributes a , b , c . I still use old string formatting, so I'd normally print these manually: print 'My object has strings a=%s, b=%s, c=%s' % (obj.a, obj.b, obj.c) Lately, my strings have been getting super long, and I'd much rather be able to simply pass the object into a string format function, something like: print 'My object has strings a=%a, b=%b, c=%c'.format(obj) However, the syntax is incorrect. Is this possible? You can use the .attribute_name notation inside the format fields themselves: print 'My object has strings a={0.a}, b={0.b}, c={0.c}'.format(obj

Formatting a string with string.Format(“{0:00}”

巧了我就是萌 提交于 2019-12-03 09:39:59
I have just taken over some code and I see this used a lot. It seems to take the integer and create a string looking like "01", "02" etc. What I am not sure of is the convention used here. Why is the format {0:00} and not {00} ? string.Format("{0:00}", int.Parse(testVal) + 1); The first 0 is the placeholder, means the first parameter. 00 is an actual format. For example it could be like this: var result = string.Format("{0:00} - {1:00}", 5, 6); result will be 05 - 06 . So the first 0 is means take the first parameter 5, while 1 means to take parameter 6. The format is {index[,length][