string-formatting

Most Pythonic way to print *at most* some number of decimal places [duplicate]

ぐ巨炮叔叔 提交于 2019-11-26 10:01:32
问题 This question already has answers here : Formatting floats in Python without trailing zeros (15 answers) Closed 5 years ago . I want to format a list of floating-point numbers with at most, say, 2 decimal places. But, I don\'t want trailing zeros, and I don\'t want trailing decimal points. So, for example, 4.001 => 4 , 4.797 => 4.8 , 8.992 => 8.99 , 13.577 => 13.58 . The simple solution is (\'%.2f\' % f).rstrip(\'.0\') (\'%.2f\' % f).rstrip(\'0\').rstrip(\'.\') . But, that looks rather ugly

Print floating point values without leading zero

一曲冷凌霜 提交于 2019-11-26 09:40:58
问题 Trying to use a format specifier to print a float that will be less than 1 without the leading zero. I came up with a bit of a hack but I assume there is a way to just drop the leading zero in the format specifier. I couldn\'t find it in the docs. Issue >>> k = .1337 >>> print \"%.4f\" % k \'0.1337\' Hack >>> print (\"%.4f\" % k) [1:] \'.1337\' 回答1: As much as I like cute regex tricks, I think a straightforward function is the best way to do this: def formatFloat(fmt, val): ret = fmt % val if

Convert Java Date to UTC String

≯℡__Kan透↙ 提交于 2019-11-26 09:39:25
问题 The java.util.Date toString() method displays the date in the local time zone. There are several common scenarios where we want the data to be printed in UTC, including logs, data export and communication with external programs. What\'s the best way to create a String representation of java.util.Date in UTC? How to replace the j.u.Date’s toString() format, which isn\'t sortable (thanks, @JonSkeet!) with a better format? Addendum I think that the standard way of printing the date in a custom

String.Format not work in TypeScript

巧了我就是萌 提交于 2019-11-26 09:36:11
问题 String.Format does not work in TypeScript . Error: The property \'format\' does not exist on value of type \'{ prototype: String; fromCharCode(...codes: number[]): string; (value?: any): string; new(value?: any): String; }\'. attributes[\"Title\"] = String.format( Settings.labelKeyValuePhraseCollection[\"[WAIT DAYS]\"], originalAttributes.Days ); 回答1: You can declare it yourself quite easily: interface StringConstructor { format: (formatString: string, ...replacement: any[]) => string; }

python format string unused named arguments [duplicate]

筅森魡賤 提交于 2019-11-26 09:25:32
问题 This question already has an answer here: partial string formatting 16 answers Let\'s say I have: action = \'{bond}, {james} {bond}\'.format(bond=\'bond\', james=\'james\') this wil output: \'bond, james bond\' Next we have: action = \'{bond}, {james} {bond}\'.format(bond=\'bond\') this will output: KeyError: \'james\' Is there some workaround to prevent this error to happen, something like: if keyrror: ignore, leave it alone (but do parse others) compare format string with available named

Logging variable data with new format string

泪湿孤枕 提交于 2019-11-26 09:16:27
问题 I use logging facility for python 2.7.3. Documentation for this Python version say: the logging package pre-dates newer formatting options such as str.format() and string.Template. These newer formatting options are supported... I like \'new\' format with curly braces. So i\'m trying to do something like: log = logging.getLogger(\"some.logger\") log.debug(\"format this message {0}\", 1) And get error: TypeError: not all arguments converted during string formatting What I miss here? P.S. I don

Add commas or spaces to group every three digits

瘦欲@ 提交于 2019-11-26 09:00:05
问题 I have a function to add commas to numbers: function commafy( num ) { num.toString().replace( /\\B(?=(?:\\d{3})+)$/g, \",\" ); } Unfortunately, it doesn\'t like decimals very well. Given the following usage examples, what is the best way to extend my function? commafy( \"123\" ) // \"123\" commafy( \"1234\" ) // \"1234\" // Don\'t add commas until 5 integer digits commafy( \"12345\" ) // \"12,345\" commafy( \"1234567\" ) // \"1,234,567\" commafy( \"12345.2\" ) // \"12,345.2\" commafy( \"12345

When to use %r instead of %s in Python? [duplicate]

独自空忆成欢 提交于 2019-11-26 07:51:44
问题 This question already has an answer here: what's the meaning of %r in python 9 answers On Learn Python the Hard Way page 21, I see this code example: x = \"There are %d types of people.\" % 10 ... print \"I said: %r.\" % x Why is %r used here instead of %s ? When would you use %r , and when would you use %s ? 回答1: The %s specifier converts the object using str(), and %r converts it using repr(). For some objects such as integers, they yield the same result, but repr() is special in that (for

Html.DisplayFor decimal format?

我的未来我决定 提交于 2019-11-26 07:41:18
问题 I have a decimal value for example: 59625879,00 I want to show this value like this: 59.625,879 or 59625,879 How can I do this with @Html.DisplayFor(x => x.TAll, String.Format()) ? Thanks. 回答1: Decorate your view model property with the [DisplayFormat] attribute and specify the desired format: [DisplayFormat(DataFormatString = "{0:N}", ApplyFormatInEditMode = true)] public decimal TAll { get; set; } and then in your view: @Html.DisplayFor(x => x.TAll) Another possibility if you don't want to

Display a decimal in scientific notation

偶尔善良 提交于 2019-11-26 07:38:22
How can I display this: Decimal('40800000000.00000000000000') as '4.08E+10'? I've tried this: >>> '%E' % Decimal('40800000000.00000000000000') '4.080000E+10' But it has those extra 0's. eumiro from decimal import Decimal '%.2E' % Decimal('40800000000.00000000000000') # returns '4.08E+10' In your '40800000000.00000000000000' there are many more significant zeros that have the same meaning as any other digit. That's why you have to tell explicitly where you want to stop. If you want to remove all trailing zeros automatically, you can try: def format_e(n): a = '%E' % n return a.split('E')[0]