string-formatting

java decimal String format

假装没事ソ 提交于 2019-11-26 22:04:35
I need to format a decimal value to a string where i always display at lease 2 decimals and at most 4. so for example "34.49596" would be "34.4959" "49.3" would be "49.30" can this be done using the String.format command? Or is there an easier/better way to do this in java. You want java.text.DecimalFormat. DecimalFormat df = new DecimalFormat("0.00##"); String result = df.format(34.4959); Yes you can do it with String.format : String result = String.format("%.2f", 10.0 / 3.0); // result: "3.33" result = String.format("%.3f", 2.5); // result: "2.500" Here is a small code snippet that does the

Binding StringFormat

 ̄綄美尐妖づ 提交于 2019-11-26 21:17:51
问题 I have a collection of textblocks that I'm going to be showing and I'm needing the text of each textblock to be displayed differently. I'm currently saving the format string in the tag property and I'm needing to display the text in this format. How do I bind the StringFormat section? Something like the section below: <TextBlock Tag="{Binding MyFormatString}" Text="{Binding MyProperty, StringFormat='{}{0:MyTag}'}" /> 回答1: Since BindingBase.StringFormat is not a dependency property, I do not

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

断了今生、忘了曾经 提交于 2019-11-26 21:14:28
This question already has an answer here: what's the meaning of %r in python 7 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 ? Ben James 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 types where this is possible) it conventionally returns a result that is valid Python syntax, which

Change default float print format

自古美人都是妖i 提交于 2019-11-26 20:56:14
问题 I've some lists and more complex structures containing floats. When printing them, I see the floats with a lot of decimal digits, but when printing, I don't need all of them. So I would like to define a custom format (e.g. 2 or 3 decimals) when floats are printed. I need to use floats and not Decimal. Also, I'm not allowed to truncate/round floats. Is there a way to change the default behavior? 回答1: You are not allowed to monkeypatch C types, like Ignacio said. However, if you are terribly

Javascript equivalent to python's .format()

拟墨画扇 提交于 2019-11-26 20:48:25
问题 I would like a javascript function that mimics the python .format() function that works like .format(*args, **kwargs) A previous question gives a possible (but not complete) solution for '.format(*args) JavaScript equivalent to printf/string.format I would like to be able to do "hello {} and {}".format("you", "bob" ==> hello you and bob "hello {0} and {1}".format("you", "bob") ==> hello you and bob "hello {0} and {1} and {a}".format("you", "bob",a="mary") ==> hello you and bob and mary "hello

Convert Java Date to UTC String

若如初见. 提交于 2019-11-26 20:29:16
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 format and time zone is quite tedious: final Date date = new Date(); final String ISO_FORMAT = "yyyy-MM

Python - convert list of tuples to string

孤街醉人 提交于 2019-11-26 20:25:01
问题 Which is the most pythonic way to convert a list of tuples to string? I have: [(1,2), (3,4)] and I want: "(1,2), (3,4)" My solution to this has been: l=[(1,2),(3,4)] s="" for t in l: s += "(%s,%s)," % t s = s[:-1] Is there a more pythonic way to do this? 回答1: you might want to use something such simple as: >>> l = [(1,2), (3,4)] >>> str(l).strip('[]') '(1, 2), (3, 4)' .. which is handy, but not guaranteed to work correctly 回答2: You can try something like this (see also on ideone.com): myList

Add commas or spaces to group every three digits

血红的双手。 提交于 2019-11-26 20:16:36
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.6789" ) // "12,345.6789" // Again, nothing until 5 commafy( ".123456" ) // ".123 456" // Group with spaces (no leading digit)

String formatting options: pros and cons

不想你离开。 提交于 2019-11-26 20:10:07
问题 These are two very popular ways of formatting a string in Python. One is using a dict : >>> 'I will be %(years)i on %(month)s %(day)i' % {'years': 21, 'month': 'January', 'day': 23} 'I will be 21 on January 23' And the other one using a simple tuple : >>> 'I will be %i on %s %i' % (21, 'January', 23) 'I will be 21 on January 23' The first one is way more readable, but the second one is faster to write. I actually use them indistinctly. What are the pros and cons of each one? regarding

How to left align a fixed width string?

眉间皱痕 提交于 2019-11-26 19:50:20
I just want fixed width columns of text but the strings are all padded right, instead of left!!? sys.stdout.write("%6s %50s %25s\n" % (code, name, industry)) produces BGA BEGA CHEESE LIMITED Food Beverage & Tobacco BHP BHP BILLITON LIMITED Materials BGL BIGAIR GROUP LIMITED Telecommunication Services BGG BLACKGOLD INTERNATIONAL HOLDINGS LIMITED Energy but we want BGA BEGA CHEESE LIMITED Food Beverage & Tobacco BHP BHP BILLITON LIMITED Materials BGL BIGAIR GROUP LIMITED Telecommunication Services BGG BLACKGOLD INTERNATIONAL HOLDINGS LIMITED Energy You can prefix the size requirement with - to