Named placeholders in string formatting

StrSubstitutor of jakarta commons lang is a light weight way of doing this provided your values are already formatted correctly. http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/text/StrSubstitutor.html Map<String, String> values = new HashMap<String, String>(); values.put(“value”, x); values.put(“column”, y); StrSubstitutor sub = new StrSubstitutor(values, “%(“, “)”); String result = sub.replace(“There’s an incorrect value ‘%(value)’ in column # %(column)”); The above results in: … Read more

Should I use Java’s String.format() if performance is important?

I took hhafez code and added a memory test: private static void test() { Runtime runtime = Runtime.getRuntime(); long memory; … memory = runtime.freeMemory(); // for loop code memory = memory-runtime.freeMemory(); I run this separately for each approach, the ‘+’ operator, String.format and StringBuilder (calling toString()), so the memory used will not be affected by … Read more

How to format strings in Java

Take a look at String.format. Note, however, that it takes format specifiers similar to those of C’s printf family of functions — for example: String.format(“Hello %s, %d”, “world”, 42); Would return “Hello world, 42”. You may find this helpful when learning about the format specifiers. Andy Thomas-Cramer was kind enough to leave this link in … Read more

Format in Kotlin string templates

Unfortunately, there’s no built-in support for formatting in string templates yet, as a workaround, you can use something like: “pi = ${pi.format(2)}” the .format(n) function you’d need to define yourself as fun Double.format(digits: Int) = “%.${digits}f”.format(this) This will work only in Kotlin/JVM. There’s clearly a piece of functionality here that is missing from Kotlin at … Read more

Format a datetime into a string with milliseconds

To get a date string with milliseconds, use [:-3] to trim the last three digits of %f (microseconds): >>> from datetime import datetime >>> datetime.utcnow().strftime(‘%Y-%m-%d %H:%M:%S.%f’)[:-3] ‘2022-09-24 10:18:32.926’ Or slightly shorter: >>> from datetime import datetime >>> datetime.utcnow().strftime(‘%F %T.%f’)[:-3]

How can I format a decimal to always show 2 decimal places?

You should use the new format specifications to define how your value should be represented: >>> from math import pi # pi ~ 3.141592653589793 >>> ‘{0:.2f}’.format(pi) ‘3.14’ The documentation can be a bit obtuse at times, so I recommend the following, easier readable references: the Python String Format Cookbook: shows examples of the new-style .format() … Read more

Using String Format to show decimal up to 2 places or simple integer

Sorry for reactivating this question, but I didn’t find the right answer here. In formatting numbers you can use 0 as a mandatory place and # as an optional place. So: // just two decimal places String.Format(“{0:0.##}”, 123.4567); // “123.46” String.Format(“{0:0.##}”, 123.4); // “123.4” String.Format(“{0:0.##}”, 123.0); // “123” You can also combine 0 with #. … Read more