string-formatting

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

大憨熊 提交于 2019-11-28 21:03:55
When formatting a number to be printed, 12 digit numbers are being formatted with a colon immediately after the dot. Why is this happening? This is Python 2.7 on an AIX system. $ uname -a ; /opt/bin/python2.7 AIX myserver 1 6 00F6A5CC4C00 Python 2.7.12 (default, Sep 29 2016, 12:02:17) [C] on aix5 Type "help", "copyright", "credits" or "license" for more information. >>> '{0:.10f}'.format(123456789012) '123456789011.:000000000' >>> from decimal import Decimal >>> u=123456789012 >>> print "%.10f" % Decimal(u) 123456789011.:000000000 Further information: It is not every 12 digit number: >>> for x

How to truncate a string using str.format in Python?

安稳与你 提交于 2019-11-28 21:02:23
How to truncate a string using str.format in Python? Is it even possible? There is a width parameter mentioned in the Format Specification Mini-Language : format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type] ... width ::= integer ... But specifying it apparently only works for padding, not truncating: >>> '{:5}'.format('aaa') 'aaa ' >>> '{:5}'.format('aaabbbccc') 'aaabbbccc' So it's more a minimal width than width really. I know I can slice strings, but the data I process here is completely dynamic, including the format string and the args that go in. I cannot just go and

Return a tuple of arguments to be fed to string.format()

末鹿安然 提交于 2019-11-28 20:09:31
Currently, I'm trying to get a method in Python to return a list of zero, one, or two strings to plug into a string formatter, and then pass them to the string method. My code looks something like this: class PairEvaluator(HandEvaluator): def returnArbitrary(self): return ('ace', 'king') pe = PairEvaluator() cards = pe.returnArbitrary() print('Two pair, {0}s and {1}s'.format(cards)) When I try to run this code, the compiler gives an IndexError: tuple index out of range. How should I structure my return value to pass it as an argument to .format() ? print('Two pair, {0}s and {1}s'.format(*cards

How to provide custom string placeholder for string format

安稳与你 提交于 2019-11-28 20:01:01
问题 I have a string string str ="Enter {0} patient name"; I am using string.format to format it. String.Format(str, "Hello"); Now if i want patient also to be retrieved from some config then I need to change str to something like "Enter {0} {1} name" . So it will replace the {1} with second value. The problem is that I want instead of {1} some other format something like {pat} . But when I try to use, it throws an error. The reason I want a different format is that there are lot of files I need

The simplest way of printing a portion of a char[] in C

旧时模样 提交于 2019-11-28 18:16:26
Let's say I have a char* str = "0123456789" and I want to cut the first and the last three letters and print just the middle, what is the simplest, and safest, way of doing it? Now the trick: The portion to cut and the portion to print are of variable size, so I could have a very long char*, or a very small one. Robert Gamble You can use printf() , and a special format string: char *str = "0123456789"; printf("%.6s\n", str + 1); The precision in the %s conversion specifier specifies the maximum number of characters to print. You can use a variable to specify the precision at runtime as well:

Does Python do variable interpolation similar to “string #{var}” in Ruby?

試著忘記壹切 提交于 2019-11-28 18:13:05
In Python, it is tedious to write: print "foo is" + bar + '.' Can I do something like this in Python? print "foo is #{bar}." Sean Vieira Python 3.6+ does have variable interpolation - prepend an f to your string: f"foo is {bar}" For versions of Python below this (Python 2 - 3.5) you can use str.format to pass in variables: # Rather than this: print("foo is #{bar}") # You would do this: print("foo is {}".format(bar)) # Or this: print("foo is {bar}".format(bar=bar)) # Or this: print("foo is %s" % (bar, )) # Or even this: print("foo is %(bar)s" % {"bar": bar}) Python 3.6 will have has literal

Objective-C formatting string for boolean?

谁说胖子不能爱 提交于 2019-11-28 18:04:08
What formatter is used for boolean values? EDIT: Example: NSLog(@" ??", BOOL_VAL); , what is ?? ? One way to do it is to convert to strings (since there are only two possibilities, it isn't hard): NSLog(@" %s", BOOL_VAL ? "true" : "false"); I don't think there is a format specifier for boolean values. I would recommend NSLog(@"%@", boolValue ? @"YES" : @"NO"); because, um, BOOL s are called YES or NO in Objective-C. Use the integer formatter %d , which will print either 0 or 1 : NSLog(@"%d", myBool); In Objective-C, the BOOL type is just a signed char. From <objc/objc.h> : typedef signed char

How can I format a decimal bound to TextBox without angering my users?

笑着哭i 提交于 2019-11-28 17:20:50
问题 I'm trying to display a formatted decimal in a TextBox using data binding in WPF. Goals Goal 1: When setting a decimal property in code, display 2 decimal places in the TextBox. Goal 2: When a user interacts with (types in) the TextBox, don't piss him/her off. Goal 3: Bindings must update source on PropertyChanged. Attempts Attempt 1: No formatting. Here we're starting nearly from scratch. <TextBox Text="{Binding Path=SomeDecimal, UpdateSourceTrigger=PropertyChanged}" /> Violates Goal 1.

JavaScript Chart.js - Custom data formatting to display on tooltip

喜欢而已 提交于 2019-11-28 17:11:21
I have looked at various documentation and similar questions on here, but cannot seem to find the particular solution. Apologies if I have missed anything obvious or have repeated this question! As a bit of background info, I have implemented 4 graphs using the Chart.js plugin and passed in the required data using PHP from a database. This is all working correctly and is fine. My problem is I need to display the data in the tooltips as formatted data aka. as numeric with %. As an example, one of my data from database is -0.17222. I have formatted it as a percentage to display in my table and

Format in kotlin string templates

丶灬走出姿态 提交于 2019-11-28 17:07:28
Kotlin has an excellent feature called string templates. I really love it. val i = 10 val s = "i = $i" // evaluates to "i = 10" But is it possible to have any formatting in the templates? For example, I would like to format Double in string templates in kotlin, at least to set a number of digits after a decimal separator: val pi = 3.14159265358979323 val s = "pi = $pi??" // How to make it "pi = 3.14"? 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