python format string thousand separator with spaces
Here is bad but simple solution if you don’t want to mess with locale: ‘{:,}’.format(1234567890.001).replace(‘,’, ‘ ‘)
Here is bad but simple solution if you don’t want to mess with locale: ‘{:,}’.format(1234567890.001).replace(‘,’, ‘ ‘)
yes, %c will print a single char: printf(“%c”, ‘h’); also, putchar/putc will work too. From “man putchar”: #include <stdio.h> int fputc(int c, FILE *stream); int putc(int c, FILE *stream); int putchar(int c); * fputc() writes the character c, cast to an unsigned char, to stream. * putc() is equivalent to fputc() except that it may … Read more
You can follow the recommendation in PEP 3101 and use a subclass Formatter: import string class BlankFormatter(string.Formatter): def __init__(self, default=””): self.default=default def get_value(self, key, args, kwds): if isinstance(key, str): return kwds.get(key, self.default) else: return string.Formatter.get_value(key, args, kwds) kwargs = {“name”: “mark”, “adj”: “mad”} fmt=BlankFormatter() print fmt.format(“My name is {name} and I’m really {adj}.”, **kwargs) # … Read more
They are used for formatting strings. %s acts a placeholder for a string while %d acts as a placeholder for a number. Their associated values are passed in via a tuple using the % operator. name=”marcog” number = 42 print ‘%s %d’ % (name, number) will print marcog 42. Note that name is a string … Read more
Number after %0 here defines full width including decimal point, so you need to change it to 7: System.out.format(“%07.3f”, 1.23456789);
To get rid of the complex if and switch constructs you can use a Dictionary lookup for the correct format string based on TotalSeconds and a CustomFormatter to format the supplied Timespan accordingly. public string GetReadableTimespan(TimeSpan ts) { // formats and its cutoffs based on totalseconds var cutoff = new SortedList<long, string> { {59, “{3:S}” … Read more
Templates are meant to be simpler than the the usual string formatting, at the cost of expressiveness. The rationale of PEP 292 compares templates to Python’s %-style string formatting: Python currently supports a string substitution syntax based on C’s printf() ‘%’ formatting character. While quite rich, %-formatting codes are also error prone, even for experienced … Read more
In Scala 2.10 you can use string interpolation. val height = 1.9d val name = “James” println(f”$name%s is $height%2.2f meters tall”) // James is 1.90 meters tall
Well I would atleast clean it up as follows: print “%.2f kg = %.2f lb = %.2f gal = %.2f l” % (var1, var2, var3, var4)
The problem is that those { and } characters you have there don’t specify a key for formatting. You need to double them up, so change your code to: addr_list_formatted.append(“”” “{0}” {{ “gamedir” “str” “address” “{1}” }} “””.format(addr_list_idx, addr))