Why can “%.10f” % Decimal(u) emit a string with a literal colon?

Although not an “answer”, I can give you my results on a slightly newer version running on AIX. Sorry that I’m unable to replicate your problem. [lholtscl@ibm3 ~]$ python Python 2.7.13 (default, Sep 7 2017, 21:08:50) [C] on aix7 Type “help”, “copyright”, “credits” or “license” for more information. >>> print “%.10f” % 123456789012 123456789012.0000000000 >>> … Read more

How to get Python to gracefully format None and non-existing fields [duplicate]

The recommendation in PEP 3101 is to subclass Formatter: import string class PartialFormatter(string.Formatter): def __init__(self, missing=’~~’, bad_fmt=”!!”): self.missing, self.bad_fmt=missing, bad_fmt def get_field(self, field_name, args, kwargs): # Handle a key not found try: val=super(PartialFormatter, self).get_field(field_name, args, kwargs) # Python 3, ‘super().get_field(field_name, args, kwargs)’ works except (KeyError, AttributeError): val=None,field_name return val def format_field(self, value, spec): # handle … Read more

How do I customize output of a custom type using printf?

It looks like the Right Way to do this in F# 2.0 is by using the StructuredFormatDisplay attribute, for example: [<StructuredFormatDisplay(“hello {a}”)>] type myType = {a: int} In this example, instead of the default {a = 42;}, you would get hello 42. This works the same way for object, record, and union types. And although … Read more

Can I format NULL values in string.Format?

You can define a custom formatter that returns “NULL” if the value is null and otherwise the default formatted string, e.g.: foreach (var value in new[] { 123456.78m, -123456.78m, 0m, (decimal?)null }) { string result = string.Format( new NullFormat(), “${0:#,000.00;(#,000.00);ZERO}”, value); Console.WriteLine(result); } Output: $123.456,78 $(123.456,78) $ZERO $NULL Custom Formatter: public class NullFormat : IFormatProvider, … Read more

Log4j Implicit String Formatting

slf4j’s api provides “parameterized logging”, which allows you to do exactly that, although with a slightly different syntax. The example there is: logger.debug(“Value {} was inserted between {} and {}.”, newVal, below, above); For an implementation, you can use Logback which implements slf4j natively, or the slf4j bindings to connect with log4j or other loggers. … Read more