string-formatting

PHP, strip_tags stripping \n in text area. How to stop it?

喜欢而已 提交于 2019-12-04 03:33:13
问题 I would like to be able to accept \n or \r\n and convert them to <br /> for use in the page. Although when a user submits a text area with new paragraphs, the strip_tags function seems to strip them right out. Anything I can do to keep these in the string? Thanks!!! 回答1: You can use nl2br to add the BR line break element to line break character sequences: $html = nl2br($plain); Note that the BR elements are just added: nl2br("foo\nbar") === "foo\n<br />bar" And to prevent strip_tags to remove

Format a number containing a decimal point with leading zeroes

喜夏-厌秋 提交于 2019-12-04 03:25:00
I want to format a number with a decimal point in it with leading zeros. This >>> '3.3'.zfill(5) 003.3 considers all the digits and even the decimal point. Is there a function in python that considers only the whole part? I only need to format simple numbers with no more than five decimal places. Also, using %5f seems to consider trailing instead of leading zeros. Starting with a string as your example does, you could write a small function such as this to do what you want: def zpad(val, n): bits = val.split('.') return "%s.%s" % (bits[0].zfill(n), bits[1]) >>> zpad('3.3', 5) '00003.3' Is that

Format C# Double to Scientfic Notation in powers with multiples of three

跟風遠走 提交于 2019-12-04 03:24:21
问题 I'm trying to format some large numbers in scientific format, but I need the power in multiples of three. Is there a recommended way to do this? I have a range of numbers in a table and instead of true scientific format (with a single digit before the decimal point) I'm happy to have that change in order to have a power with a multiple of three, for example: 3.123e+003 19.523e+003 Rather than: 3.123e+003 1.952e+004 Having all the powers as multiples of three makes my table easier to scan, I

Python string formatting - old `%` vs new `str.format`

两盒软妹~` 提交于 2019-12-04 03:16:28
New formatting lets us do this: '{:.<12}'.format('##') - optional fill character. Can we do that using old formatting? (I know we can fill with spaces '%-12s' % '##' ) Also, old formatting lets us do this: '%-*s' % (12, '##') - variable length. Can we do that using new formatting? For doing variable length using new-format , you can use nesting of replacements - >>> '{:{}<{}}'.format('##','.',12) '##..........' >>> '{:{}<{}}'.format('##','-',12) '##----------' >>> '{:{}<{}}'.format('##','-',20) '##------------------' Even spaces as fill character - >>> '{:{}<{}}'.format('##',' ',20) '## '

Format decimal value to currency with 2 decimal places [duplicate]

馋奶兔 提交于 2019-12-04 03:08:27
This question already has answers here : Closed 5 years ago . How do I round a decimal value to 2 decimal places (for output on a page) (16 answers) I am getting data from a csv file and parsing it to my application. In my csv file I have a column price whose value I use as of course the price of an item in my project. However, the price in the csv file does not contain trailing 0s, For example, if the price of and item is $5.00 , the csv file has it as $5 , if the price is $9.60 , the csv has it as $9.6 . Other prices such as $9.56 are fine though. This is how I retrieve the price from the

Format floating point number with leading zeros [duplicate]

…衆ロ難τιáo~ 提交于 2019-12-04 02:59:43
问题 This question already has answers here : printf how to do floating points with leading zeros (3 answers) Closed 4 years ago . Why does System.out.format("%03.3f", 1.23456789); print 1.235 instead of 001.235 ? How has my format string to look like to get 001.235 as output of the following code line? System.out.format(format, 1.23456789); 回答1: 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); 回答2:

What do the curly braces mean in C# strings?

我与影子孤独终老i 提交于 2019-12-04 02:48:30
while (rdr.Read()) { Console.WriteLine("Product: {0,-35} Total: {1,2}", rdr["ProductName"], rdr["Total"]); } What does {0,-35} mean in this code? A more simple line would be: Console.WriteLine("{0}", 5); The function accepts any number of arguments. They will be inserted into the string at the corresponding index. In this case, index zero holds the integer 5. The result is the string "5". Now, you have the option the specify a format string as well as an index. Like so: Console.WriteLine("{0:0.00}", 5); This formats the 5 with 0.00 , resulting in 5.00 . Thats the case for numbers, but I think

Python 2.6+ str.format() and regular expressions

六月ゝ 毕业季﹏ 提交于 2019-12-04 02:46:47
问题 Using str.format() is the new standard for formatting strings in Python 2.6, and Python 3. I've run into an issue when using str.format() with regular expressions. I've written a regular expression to return all domains that are a single level below a specified domain or any domains that are 2 levels below the domain specified, if the 2nd level below is www... Assuming the specified domain is delivery.com, my regex should return a.delivery.com, b.delivery.com, www.c.delivery.com ... but it

Formatting output as table

懵懂的女人 提交于 2019-12-04 02:39:10
问题 Example input: [('b', 'c', 4), ('l', 'r', 5), ('i', 'a', 6), ('c', 't', 7), ('a', '$', 8), ('n', '$', 9)] [0] contains the vertical heading, [1] contains the horizontal heading. Example output: c r a t $ $ b 4 l 5 i 6 c 7 a 8 n 9 Note: given enough tuples the entire table could be filled :P How do I format output as a table in Python using [preferably] one line of code? 回答1: Here's an answer for your revised question: data = [ ['A','a','1'], ['B','b','2'], ['C','c','3'], ['D','d','4'] ] #

Can't use apostrophe in StringFormat of a XAML binding?

随声附和 提交于 2019-12-04 02:28:26
I'm trying use StringFormat to insert apostrophies (apostrophe's?) around a value that is bound to a TextBlock: <TextBlock Text="{Binding MyValue, StringFormat='The value is &apos;{0}&apos;'}"/> However, I get a compile error: Names and Values in a MarkupExtension cannot contain quotes. The MarkupExtension arguments ' MyValue, StringFormat='The value is '{0}''}' are not valid. I do notice that it does work for quotes though: <TextBlock Text="{Binding MyValue, StringFormat='The value is "{0}"'}"/> Is this a bug with StringFormat? I'm not sure if it's a bug, but I tested this method, and it