string-formatting

How to left align a fixed width string?

≯℡__Kan透↙ 提交于 2019-11-26 07:27:10
问题 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

Add separator to string at every N characters?

a 夏天 提交于 2019-11-26 07:25:01
问题 I have a string which contains binary digits. How to separate string after each 8 digit? Suppose the string is: string x = \"111111110000000011111111000000001111111100000000\"; I want to add a separator like ,(comma) after each 8 character. output should be : \"11111111,00000000,11111111,00000000,11111111,00000000,\" Then I want to send it to a list<> last 8 char 1st then the previous 8 chars(excepting ,) and so on. How can I do this? 回答1: Regex.Replace(myString, ".{8}", "$0,"); If you want

Format numbers to strings in Python

浪尽此生 提交于 2019-11-26 07:21:19
问题 I need to find out how to format numbers as strings. My code is here: return str(hours)+\":\"+str(minutes)+\":\"+str(seconds)+\" \"+ampm Hours and minutes are integers, and seconds is a float. the str() function will convert all of these numbers to the tenths (0.1) place. So instead of my string outputting \"5:30:59.07 pm\", it would display something like \"5.0:30.0:59.1 pm\". Bottom line, what library / function do I need to do this for me? 回答1: Starting with Python 3.6, formatting in

Named placeholders in string formatting

非 Y 不嫁゛ 提交于 2019-11-26 06:59:04
问题 In Python, when formatting string, I can fill placeholders by name rather than by position, like that: print \"There\'s an incorrect value \'%(value)s\' in column # %(column)d\" % \\ { \'value\': x, \'column\': y } I wonder if that is possible in Java (hopefully, without external libraries)? 回答1: StrSubstitutor of jakarta commons lang is a light weight way of doing this provided your values are already formatted correctly. http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org

What&#39;s the difference between %s and %d in Python string formatting?

社会主义新天地 提交于 2019-11-26 06:56:22
问题 I don\'t understand what %s and %d do and how they work. 回答1: 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 (%s) and number is an integer (%d for decimal). See https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting for

java decimal String format

旧街凉风 提交于 2019-11-26 06:35:49
问题 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. 回答1: You want java.text.DecimalFormat. DecimalFormat df = new DecimalFormat("0.00##"); String result = df.format(34.4959); 回答2: Yes you can do it with String.format : String result = String.format("%.2f", 10.0 / 3

How can I format a number into a string with leading zeros?

孤街浪徒 提交于 2019-11-26 05:56:31
问题 I have a number that I need to convert to a string. First I used this: Key = i.ToString(); But I realize it\'s being sorted in a strange order and so I need to pad it with zeros. How could I do this? 回答1: Rather simple: Key = i.ToString("D2"); D stands for "decimal number", 2 for the number of digits to print. 回答2: See String formatting in C# for some example uses of String.Format Actually a better example of formatting int String.Format("{0:00000}", 15); // "00015" or use String

How can I convert 24 hour time to 12 hour time?

给你一囗甜甜゛ 提交于 2019-11-26 05:35:11
问题 I have the following 24-hour times: {\'Wed\': \'10:30 - 21:00\', \'Sun\': \'10:30 - 21:00\', \'Thu\': \'10:30 - 21:00\', \'Mon\': \'10:30 - 21:00\', \'Fri\': \'10:30 - 22:00\', \'Tue\': \'10:30 - 21:00\', \'Sat\': \'10:30 - 22:00\'} How can I convert this to 12-hour time? {\'Wed\': \'10:30 AM - 09:00 PM\', \'Sun\': \'10:30 AM - 09:00 PM\', \'Thu\': \'10:30 AM - 09:00 PM\', \'Mon\': \'10:30 AM - 09:00 PM\', \'Fri\': \'10:30 AM- 10:00 PM\', \'Tue\': \'10:30 AM- 09:00 PM\', \'Sat\': \'10:30 AM -

WPF StringFormat={0:C} showing as dollars

こ雲淡風輕ζ 提交于 2019-11-26 05:28:58
问题 Why does this line of code <TextBlock Text=\"{Binding Net, StringFormat=c}\"/> Output the result as $xx.xx when all my regional settings are set to UK. I expect it to output it as £xx.xx. Any ideas? I have tried different variations of the stringformat including StringFormat={}{0:C} but still get the same result. Thanks for looking. 回答1: I'm not sure if this has been fixed in .NET 4, but WPF has never picked up the current culture when rendering things like currency or dates. It's something I

How can I load 100 files with similar names and/or string in just one step in MATLAB?

為{幸葍}努か 提交于 2019-11-26 04:56:15
问题 I have 100 ASCII files in my directory all named as follows: int_001.ASC int_002.ASC int_003.ASC . . . int_099.ASC int_100.ASC I have to import them in MATLAB all with importdata, which should work as follows: A = importdata(\'int_001.ASC\', \' \', 9) x = A.data(:,1) y = A.data(:,2) My question is: how can I avoid writing 100 times importdata ? Is there a way to write the first string only and then have all data uploaded? Thanks 回答1: fls = dir( 'int_*.ASC' ); for fi=1:numel(fls) A{fi} =